Help - Search - Members - Calendar
Full Version: What's faster in PHP?
Invision Power Services > Community Forums > Community Web Design and Coding
ellawella
CODE
if ($ipsclass->DB->get_num_rows() != 0)


or

CODE
if ($ipsclass->DB->get_num_rows())


Which is best speed wise?
Starnox
CODE
if ($ipsclass->DB->get_num_rows())


Probably, but there won't be much in it.
Rikki
I can't believe there's any difference at all that's worth taking notice of.
_
The latter is faster.

Differences tend to be around .02 seconds.
Stephen
QUOTE(Veracon @ Apr 22 2006, 10:36 PM) *
Differences tend to be around .02 seconds.


yes and 89% of statistics are made up on the spot
_
Indeed, thanks to my PHP port of timeit. I ran 10 million tests, and I got that result.

Trust it or not, just use the latter. It's definitely not slower.
Rikki
QUOTE(Stephen @ Apr 22 2006, 10:49 PM) *
yes and 89% of statistics are made up on the spot


Actually, it's 82%. Get it right rolleyes.gif
Stephen
QUOTE(Veracon @ Apr 22 2006, 10:53 PM) *
I ran 10 million tests, and I got that result.


You didn't state that, you implied that the difference was .02 between those single 2 lines of code, which is a huge amount of time.
_
Sorry about that, I stated it now.

In case anyone should be interested, these are the averages (which are each averages of 10 million executions, measured in seconds); without != 0
CODE
0.053891332276187143  0.056015828050789196  0.054976876087067694
0.063542614484987325  0.054946164508216100  0.053413044396407372
0.056209450602615618  0.060140022806763227  0.053634445131287123


With != 0
CODE
0.073944104220004192  0.075370129276763009  0.080298788630386753
0.074197202523350825  0.078859500324426790  0.082524448164519981
0.075694000744874756  0.082475653966810114  0.085333768991262104
ellawella
Thanks guys. original.gif
_
Actually, I should note that they don't do exactly the same; with != 0, -1 will for instance be accepted. If you don't expect this result, the latter will definitely be better.
Stewart
QUOTE(Veracon @ Apr 23 2006, 02:12 PM) *
Actually, I should note that they don't do exactly the same; with != 0, -1 will for instance be accepted. If you don't expect this result, the latter will definitely be better.


True, although in this case the get_num_rows() function just wraps mysql_num_rows(), which can never return -1 original.gif
Dagur
First of all I can't believe you're wondering about the speed of one simple statement. You know you're over-optimising when you do stuff like that.

Secondily, I would love to see how you guys benchmarked that code.
Brendon Koz
I'd also like to see the PHP port of timeit. original.gif
_
It's probably a bit unfair to explicitly call it a port, since Python's version is quite a lot more advanced (and just plain awesome'r), but anyway:
http://labs.veracon.net/in/php/timeit.php
Michael Boutros
Just a note to any beginner trying to run that code, it requires PHP 5.

Offtopic: PHP 5 OOP -> PWNAGE!
Dimdum
Tell me, do you care about these 0.002 seconds?!
ellawella
In asking this question, I didn't so much want to optimize one line of code to shave off a few thousandths of a second, I just wanted to know what would be fastest in PHP in general. original.gif

It would surely apply to statements like if ($variable) or if ($variable == value) as well, yes? If you have one of these statements in a loop that executes 1,000 times, you save 2 seconds.
Dimdum
Frankly speaking, when you have a loop that executes 1,000 times it is matter. But the total time that the loop will run will be long, and your server will stop it, beacuse the execution time is limited.
After all, you're right. Although it's not so felt, it's important to make your code the fastest and the effectiviest.
_
QUOTE(Michael Boutros @ Apr 25 2006, 05:27 PM) *
Just a note to any beginner trying to run that code, it requires PHP 5.

Offtopic: PHP 5 OOP -> PWNAGE!

Ah, that's right. I thought it'd have been clear by now, but unfortunately it's far too rarely used (and therefore supported). The OOP is indeed a great improvement, but it still really isn't full-fletched enough in my opinion. It probably is enough for most people, though. original.gif

QUOTE(Dimdum @ Apr 26 2006, 06:10 AM) *
Tell me, do you care about these 0.002 seconds?!

Probably not, but why'd you NOT save them? Is it better to use those extra .002 seconds just to be able to say "I don't optimise my code?"

QUOTE(Dimdum @ Apr 26 2006, 06:38 AM) *
Frankly speaking, when you have a loop that executes 1,000 times it is matter. But the total time that the loop will run will be long, and your server will stop it, beacuse the execution time is limited.

Maybe, maybe not. I personally keep max_execution_time off, because I think it's an annoyance rather than a feature. It's cool that it's offered and all, but I hate that it's turned on by default.

Oh, in case anyone wonders, the reason I'm defining the template function as 'inner_(some number)' is that PHP for whatever reason doesn't allow redeclaration of functions, unless they're methods. Annoying, but I guess this works.
Dimdum
QUOTE(Veracon @ Apr 26 2006, 03:04 PM) *
Maybe, maybe not. I personally keep max_execution_time off, because I think it's an annoyance rather than a feature. It's cool that it's offered and all, but I hate that it's turned on by default.
I were spending a lot of time by tring to keep this max_execution_time off, but unfortunately it didn't work...
Can you be so kind and tell me how to make this max_execution_time off?
I tried to make it 100 seconds by changing the php configuration file and restarting the server, but it didn't work. It stubbornnessly stayed on the 30 seconds...
_
I just modified PHPini, and it appeared to work perfectly. blink.gif

Maybe ini_set('max_execution_time', -1) will work? I don't know, sorry.
Dimdum
It doesn't work!!!!!
By the way: hot to set is to unlimited time?
and, is there really function like ini_set?!
Chroder
You're tests say the difference is 0.2 seconds over 10 million executions. In practical terms, that means you save 0.2/10million seconds.

If you need to save 2.0x10^-8 seconds, then you should probably be using a compiled solution instead of an interpreted script.
Rikki
QUOTE(Chroder @ Apr 26 2006, 10:04 PM) *
You're tests say the difference is 0.2 seconds over 10 million executions. In practical terms, that means you save 0.2/10million seconds.

If you need to save 2.0x10^-8 seconds, then you should probably be using a compiled solution instead of an interpreted script.


I think he means on average...
Antony
ini_set() might not work, depends on your host.
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2009 Invision Power Services, Inc.