Jump to content
Larry Ullman's Book Forums

Recommended Posts

I came across this little piece the other day, thought that some of you may find it useful.

functions vs speed

 

I can't guarantee their accuracy but chatting with friends, we seem to feel that they seem about right!

 

Ones that caught my eye were:

The use of the @ supressor

file_get_contents() vs fopen(), fread(), fclose()

 

and a few others

 

Jonathon

  • Upvote 1
Link to comment
Share on other sites

I always find these articles interesting - I started reading lots of them after I read Larry's article on what makes a good programmer. One which caught my eye is:

 

require_once() is 24% faster than include()

 

I've seen benchmarking to support this but the tests looked shoddy and I've also read an article by Rasmus Lerdorf (the creator of PHP) talking about avoiding the use of _once functions:

 

Try to avoid using include_once and require_once if possible. Sometimes there is no way around using these calls, but recognize that each one costs you an extra open() syscall and hash look up.

 

If he says it it's good enough for me...

  • Upvote 1
Link to comment
Share on other sites

Thanks, Jonathon, for sharing that. I'll look at it in detail later. As for the _once() functions, someone just posted on my blog a link to a benchmark suggesting that _once() is actually faster, which makes no sense. The problem with benchmarks, and there are many, is that there are so many factors that can skew the numbers. So what is generally true is not specifically true and vice versa. And, yes, Lerdorf says not to use them, which is a strong opinion.

 

Personally, I like to emphasize best practices over benchmarks. Like: if there's no reason to think a file might be included multiple times, there's no reason to use _once(). Or: if there are no variables to be extrapolated in a string, there's no reason to use double quotes (whether or not that's also 0.26% faster).

 

Some people also unduly stress the importance of speed. If constants are slower than variables (as that link suggests), I'd still rather see constants for some things as it's better programming.

 

My two cents on the subject...

Link to comment
Share on other sites

Yes I must say its not the first and I doubt the last set of benchmarks I'll see. And there doesn't seem to be a universally accepted speed for anything really. Some of those figures listed are strange, especially _once() after you mentioned the overhead it uses. But 'apparently' its 24% faster than include.

 

I don't think anyone can really know the precise implementations on performance to be honest.

 

However, I would like to know if xcache_set() really is 1,645% faster than file_put_contents(). Just because its such huge %.

 

Some people also unduly stress the importance of speed.

 

I've seen it quite a lot around the web and forums, things like @ is really slow blah blah. It may be slower I guess, but whenever I've used it i've never thought, damn, the @ is making my script too slow, better remove it.

 

Jonathon

  • Upvote 1
Link to comment
Share on other sites

Agreed, agreed. One of the interesting things about Ruby is that the language purposefully emphasizes the time of the developer over the time of the computer (i.e., it's not all about making scripts execute as quickly as possible). It's very, very easy to upgrade to a faster computer or hard drive, but impossible to get the hour back that you spent figuring out that '' is 0.26% faster than "". (Not you, of course, but them.)

Link to comment
Share on other sites

 Share

×
×
  • Create New...