PHP: floor() and ceil() are slow!

A workmate mentioned, that floor($i) could be replaced with (int)$i.
I found this very interresting and did some researched about that.

floor($i); will return a float that will have the lower value of $i.
A cast will just convert $i to an integer which will cut of all decimals.
So by symptoms, these two methods are equivalent. Even if floor returns a float, PHP does dynamic casting of its variables.

Benchmark

Next question for me was, which one is faster?
The benchmark script can be found here:
bench_floor_and_ceil_vs_cast.phps - with highlighting
bench_floor_and_ceil_vs_cast.php.txt - without highlighting

Results

E:>php bench_floor_and_ceil_vs_cast.php
Test: floor() vs. (int)-cast function:

0.628 seconds - floor()
0.239 seconds - (int)-cast

Test: ceil() vs. +1 && (int)-cast function:

0.624 seconds - ceil()
0.284 seconds - +1 && (int)-cast

The floor() and the ceil() function are about 60% (!) slower than the cast method.

Why?

Why is that so?
I think because of the function call, more resources are needed instead of the dynamic cast which should be done by PHP very fast.

Problems

One problem i could find was: What happens with large numbers?
Something bad will happen if you (int) cast a number greater equal than 2147483648. In this case it will revert to -2147483648 because PHP handles this as some kind of overflow.
Normaly, PHP will interpret such a number as a float, but because of the cast this cant be done.
This is also the cause why floor() returns a float instead of a int.
Some more information on this topic can be found here:
http://php.net/manual/en/language.types.integer.php

No related posts.


 
 
 

2 Kommentare zu “PHP: floor() and ceil() are slow!”

  1. Robert 15. Januar 2010 um 16:29

    You are right. The function call in PHP is really slow. You should avoid function calls where ever you can, if you have to build a performant application. This idea can be lead to use language constructs for everything. For example use $str[$i] instead of substr($str, $i, 1). But if you have to loop over arrays, it is a better idea to use prefabricated functions.

    The problem with numbers out of the range of int should be known when the app is developed. So it is possible to use either the fast int cast or the slower floor(). You have to keep in mind, that this is a benchmark. In general the floor() is only called once (if it is not inside of a loop).

  2. Half Truth 25. März 2010 um 23:06

    "So by symptoms, these two methods are equivalent."
    => That is wrong!

    If the PHP floor is a real floor function it will produce following reuslt:
    floor( -1.2 ) = -2

    If (int) does really cut, as you said, it will produce following result:
    (int)-1.8 = -1

    Your testimony should only count for positive values!

    To confuse you even more:
    in VB the funktion Fix() does cut like (int) here,
    but the function Int() there ia a floor() - so don't mix it up! ;)
    Oh, and there is no ceil() in VB, it has to be produced with -Int(-x)