[wp-trac] [WordPress Trac] #22286: Enhancing backslashit performance
WordPress Trac
noreply at wordpress.org
Mon Nov 19 18:05:32 UTC 2012
#22286: Enhancing backslashit performance
-------------------------+------------------------------
Reporter: jbutkus | Owner:
Type: enhancement | Status: new
Priority: normal | Milestone: Awaiting Review
Component: Formatting | Version:
Severity: normal | Resolution:
Keywords: has-patch |
-------------------------+------------------------------
Comment (by jbutkus):
[comment:2 @nacin] - thank you, for your comment. I have updated diff with
array-style brackets for char-in-string accessor.
As for first character comparison - it is based on ASCII table, and
compiles to C code directly.
We are saving method call (JMP, more likely - long one, in assembly),
which would occur if ''is_numeric'', or ''strpbrk'' would be used.
And yes - ''is_numeric'' is slower by order of magnitude. Mostly because
{{{is_numeric( '1.8e26' )}}} is '''true''', but it has to evaluate rather
large number, thus causing a call to mathematical co-processor, instead of
relying on general CPU.
As for ''strpbrk'' - it maps to C equivalent directly (with sanity checks,
only).
Although it is also slower, than direct char comparison using order mask.
Mostly we save the iteration, which occurs during char-list traversal in
''strpbrk''.
And what you observe, when {{{$string[0] <= '9'}}} is ommited is true not
only in PHP, but in C as well:
{{{
#include <stdio.h>
int main( int argc, const char* argv[] ) {
unsigned char single_letter = 'a';
if ( argc > 1 ) {
single_letter = *(*(argv+1));
}
if ( single_letter >= '0' /*&& single_letter <= '9'*/ ) { // notice,
that second check is commented out
printf( "It is a digit: %c\n", single_letter );
} else {
printf( "It is NOT a digit: %c\n", single_letter );
}
return 0;
}
}}}
If we compile the program, and run it as following:
./tester a
It would say '''It is a digit: a'''.
Although with {{./tester $}} it would say '''It is NOT a digit: $'''.
And if we un-comment the following part: {{{&& single_letter <= '9'}}} it
would respond as expected: '''It is NOT a digit: a''' vs. '''It is a
digit: 5'''.
--
Ticket URL: <http://core.trac.wordpress.org/ticket/22286#comment:3>
WordPress Trac <http://core.trac.wordpress.org/>
WordPress blogging software
More information about the wp-trac
mailing list