Page 1 of 2
Which is better
Posted: Fri Oct 20, 2006 4:41 pm
by amir
these are items
1.2
1.56
1.765
and i can take the first part i mean before '.' in several ways like
1;
2;
3;
Code: Select all
$arr = explode(".", "1.2");
$val = arr[0];
i want to know which is better and why
i know first and 3rd having calls, so they are slow
thanks
Posted: Fri Oct 20, 2006 4:52 pm
by hawleyjr
It really depends on what you are trying to do with the returned value and what is being passed (Is it always of var type double?)
Generally I'd use
floor() for this.
Posted: Fri Oct 20, 2006 5:06 pm
by feyd
I'd go with
floor() as well.
Posted: Fri Oct 20, 2006 5:12 pm
by amir
Thanks!
I am asking which is better and why?
Any sujjestion would be very precious for me.
Posted: Fri Oct 20, 2006 5:15 pm
by feyd
"Better" is pretty subjective. How do you qualify better in this case?
Posted: Fri Oct 20, 2006 5:26 pm
by amir
You are right!
But some are obviously fast and others may be efficient.
Thats what i am asking about?
Posted: Fri Oct 20, 2006 5:29 pm
by RobertGonzalez
floor(). It returns a float back from the call so your types are the same. You are not changing the core data (as with type casting) and
explode() requires the initial call (with overhead) plus reading an var into the reurn.
Posted: Fri Oct 20, 2006 5:31 pm
by s.dot
floor is better because that's the purpose of the function! Typecasting and splitting into an array are just workarounds to do what floor already does.
Posted: Fri Oct 20, 2006 5:31 pm
by amir
Thanks All!
Posted: Fri Oct 20, 2006 5:34 pm
by Cameri
Well, since type casting is faster than a function call, in this case, floor(), I'd go with type casting.
Posted: Fri Oct 20, 2006 5:40 pm
by s.dot
the purpose of typecasting is to cast to a type, not to floor
Posted: Fri Oct 20, 2006 5:45 pm
by RobertGonzalez
scottayy wrote:the purpose of typecasting is to cast to a type, not to floor
Well said scottayy.
Posted: Fri Oct 20, 2006 5:55 pm
by amir
Thanks for this healthy discussion but if you all dont mind, please also see my this post.
viewtopic.php?t=57419
Posted: Fri Oct 20, 2006 6:08 pm
by RobertGonzalez
amir wrote:Thanks for this healthy discussion but if you all dont mind, please also see my this post.
viewtopic.php?t=57419
Please refrain from bumping other threads within a thread.
Posted: Fri Oct 20, 2006 6:22 pm
by amir
I am Sorry!
Thanks for informing me.