|« phpGolf||IPv6-test.com »|
I've been working on new performance optimizations in nanoserv 2.1.1-dev in the last few days, and I found this one interesting enough to write about it ...
Nanoserv uses buffering for all socket write operations, and until now it was using strlen() in various places to compare it with some pointer to the current buffer position. However, strlen() can be quite slow, and in some cases it can be replaced with a simple test using isset().
The two code snippets below are equivalent, because in the end all we want to know is if our pointer is inside or outside the string :
When testing strlen() vs. isset($str[$x]) using a micro benchmark, I obtained the following results :
strlen : 0.233 s
isset : 0.067 s
So isset() is about 3.5x faster here. In nanoserv 2.1.1 with all calls to strlen() changed into isset()s there is a noticeable gain of about 2-3% with the apache benchmark.