[TYPO3-core] RFC: core speedup

Bernhard Kraft kraftb at kraftb.at
Sat Mar 25 19:30:01 CET 2006


Hello,


This is a CVS patch request.


Description:
I noticed that at many places in the core a construct like:

$somewhat = pow(2, $bit);

is used to get a bit-mask where bit number $bit seen from the LSB (least significant bit) side is set.

This wastes rare CPU resource as in this case a real "raise to the power of" operation get's performed
in the CPUs ALU.

Normally when you work with bitmasks and you have to do such a thing (and even when you do mathemtical
stuff but know about this) you do this by "bit shifting" ... This is an operation which can get done
much faster by the CPU as it only has to move the bitmask to the left or right.

<< and >> are the shift-left and shift-right operators in PHP (like in C)

An example:

1 << 3	// Shift 1 to the left 3 steps == pow(2,3)

binary:

0 0 0 0 0 0 0 1 << 3
0 0 0 0 1 0 0 0		// 3 bits wider left

and you will surely know that 1000 which is 8 really is 2^3


I made a test program which compare the 2 versions:


for cases: 2 ^ ( 0 .. 31 )

kraftb at osmium ~/src $ ./powtest.php
pow() version: 3.58 seconds (100%)
shift version: 2.49 seconds (69.582230725%)

for cases: 2 ^ ( 0 .. 10 )

kraftb at osmium ~/src $ ./powtest.php
pow() version: 3.91 seconds (100%)
shift version: 2.82 seconds (72.0292317471%)


I attach the testing program and a patch which fixes this everwhere in the core where it is applicable (you can
only use bit-shifting when you want to raise 2 to the power of something)


greets,
Bernhard
-------------- next part --------------
A non-text attachment was scrubbed...
Name: powtest.php
Type: application/x-php
Size: 901 bytes
Desc: not available
Url : http://lists.netfielders.de/pipermail/typo3-team-core/attachments/20060325/16ff03fe/attachment.bin 
-------------- next part --------------
A non-text attachment was scrubbed...
Name: pow2shift_2006-03-25.diff
Type: text/x-patch
Size: 5495 bytes
Desc: not available
Url : http://lists.netfielders.de/pipermail/typo3-team-core/attachments/20060325/16ff03fe/attachment-0001.bin 


More information about the TYPO3-team-core mailing list