Discussion:
intval() vs. (int)
Thomas
2005-09-02 09:05:29 UTC
Permalink
Hi,

On checking form fields that they are of type int, what is best to use:
intval() or type casting (int)?
In terms of speed, would (int) not be better, because we save a function
call (especially on very large sql statements)?

Thanks.

Thomasx`


SPIRAL EYE STUDIOS
P.O. Box 37907, Faerie Glen, 0043

Tel: +27 12 362 3486
Fax: +27 12 362 3493
Mobile: +27 82 442 9228
Email: ***@gmx.net
Web: www.spiraleye.co.za
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Philip Hallstrom
2005-09-02 16:00:33 UTC
Permalink
Post by Thomas
intval() or type casting (int)?
In terms of speed, would (int) not be better, because we save a function
call (especially on very large sql statements)?
Time it.

On an 800mhz box doing absolutely nothing else (it's just sitting there,
honest :) a script which reads 100,000 16byte strings from a testfile
(composed of a bunch of tar balls put together just to get something
random) and then doing this:

- loop through array doing $x = $array[$i] just to read it all in once.
- loop through array doing $x = intval($array[$i])
- loop through array doing $x = (int) $array[$i]

yields this (in seconds).

nothing = 0.3619658946991
intval = 0.60399794578552
(int) = 0.48065304756165

So, for 100,000 random 16 byte strings you're saving 0.12 seconds or
0.0000012 per iteration...

So, it probably doesn't really matter :)

-philip
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Rory Browne
2005-09-02 18:47:42 UTC
Permalink
(int) seems to be faster, but not by an awful lot. Personally however
if its a case of typing five characters (int) and saving a little exec
time, or typing 8 and losing a little, then I'd perfer to go with the
five an save the exec time.

I think I remember hearing about some other consequence with using the
intval function call, but can't remember what it was.
Post by Philip Hallstrom
Post by Thomas
intval() or type casting (int)?
In terms of speed, would (int) not be better, because we save a function
call (especially on very large sql statements)?
Time it.
On an 800mhz box doing absolutely nothing else (it's just sitting there,
honest :) a script which reads 100,000 16byte strings from a testfile
(composed of a bunch of tar balls put together just to get something
- loop through array doing $x = $array[$i] just to read it all in once.
- loop through array doing $x = intval($array[$i])
- loop through array doing $x = (int) $array[$i]
yields this (in seconds).
nothing = 0.3619658946991
intval = 0.60399794578552
(int) = 0.48065304756165
So, for 100,000 random 16 byte strings you're saving 0.12 seconds or
0.0000012 per iteration...
So, it probably doesn't really matter :)
-philip
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Robert Cummings
2005-09-02 18:59:25 UTC
Permalink
Post by Rory Browne
(int) seems to be faster, but not by an awful lot. Personally however
if its a case of typing five characters (int) and saving a little exec
time, or typing 8 and losing a little, then I'd perfer to go with the
five an save the exec time.
(int) is a casting operator and so doesn't incur function initialization
overhead. Similarly using === null instead of is_null() has the same
consequences. While the savings might seem paltry, knowing the
difference between operator and functions and when you have both
available for the same task can add up to real savings in a large
application or in a heavily loaded system. I bet Rasmus makes a good
distinction between the two when doing work for Yahoo :) Maybe not
though, I've been wrong before ;)

Cheers,
Rob.
--
.------------------------------------------------------------.
| InterJinn Application Framework - http://www.interjinn.com |
:------------------------------------------------------------:
| An application and templating framework for PHP. Boasting |
| a powerful, scalable system for accessing system services |
| such as forms, properties, sessions, and caches. InterJinn |
| also provides an extremely flexible architecture for |
| creating re-usable components quickly and easily. |
`------------------------------------------------------------'
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Loading...