0

For Benchmarking PHP with iterations, I have considered a large for loop

for($j=0;$j<20000000;$j++)
    ;

Executing the same takes around 2.5 seconds in PHP 5.4 with eAccelerator enabled

The same loop in .jsp file takes around 15 milliseconds

for(int j=0;j<20000000;j++)
    ;

Why is there such a huge difference between both of them? Is there a way to make it work faster in PHP

Akash
  • 4,956
  • 11
  • 42
  • 70

2 Answers2

4

At 15ms the loop was probably optimized by the JIT. Unless you're using the HipHop VM, your PHP loop doesn't really stand a chance.

Keep in mind that the performance of a tight loop is hardly representative of relative performance in the real world with real workloads. Check out The Computer Language Benchmarks Game instead -- and even their far more meaningful measurements are to be taken with a grain of salt.

Community
  • 1
  • 1
vladr
  • 65,483
  • 18
  • 129
  • 130
  • going through the mentioned benchmarks, it does seem that PHP is lower in many aspects than Java, http://shootout.alioth.debian.org/u32/php.php – Akash Nov 12 '12 at 03:49
  • Not surprised, Java has JIT. – vladr Nov 12 '12 at 04:01
  • It's likely that the Java compiler has completely optimized out the loop. The code has no side-effect. A dead-code elimination algorithm would be able to see that. – cleong Nov 12 '12 at 04:48
  • Interestingly enough the compiler itself has not optimized the loop out. Code: 0: iconst_0 1: istore_1 2: iload_1 3: ldc #2; //int 20000000 5: if_icmpge 14 8: iinc 1, 1 11: goto 2 14: return Performance through JIT is comparable with native C/C++ (~20ms on modern harwadre.) – vladr Nov 12 '12 at 05:16
  • @vladr >> to be taken with a grain of salt << That puts the emphasis in the wrong place: the measurements shown on the benchmarks game website are exactly what they claim to be -- the problem is that they are not measurements of programs doing the task Akash is trying to solve; the danger is that Akask will generalize from those very few measurements without considering and understanding that step in his thinking. – igouy Nov 24 '12 at 16:09
0

These types of micro benchmarks should be of no concern.

Anyway this loop is slightly faster:

$i = 20000000;
while($i--);

http://benchmarksgame.alioth.debian.org/u32/compare.php?lang=java&lang2=php

igouy
  • 2,547
  • 17
  • 16
Petah
  • 45,477
  • 28
  • 157
  • 213
  • I would take 15ms over 2.5s any day of the week; "of no concern" would apply to maybe 15ms vs 100ms, but once you cross the "seconds" threshold, delays are quite noticeable. – newfurniturey Nov 12 '12 at 03:39
  • @newfurniturey Looping something 20 million times, the actual loop time should be insignificant. – Petah Nov 12 '12 at 03:41