Functions of cast or direct cast. What is the best option?

4

In PHP, I realize that it is possible to do some things in a number of ways.

One of those that comes to my attention is the functions and functionalities related to type conversion. You can use both the type keyword and use functions.

For example:

 $var = '1';

 var_dump((int) $var); // int(1)

 var_dump(intval($var)); // int(1);


 var_dump(strval($var)); // string(1)

 var_dump((string) $var); // string(1);

Given that feature calls often consume more resources than using a language constructor or the cast operator, I would like to know:

  • What is the need to have these two features?

  • For a cast from string to int , for example, what would be recommended? Use (int) or intval ?

  • Given that call functions usually consume more resources than calling a constructor, is there any situation that will really need to use type conversion functions, rather than cast?

    li>
asked by anonymous 18.08.2016 / 16:19

2 answers

4
  

Given that function calls often consume more resources than using a language constructor or the cast operator

This is false, at least conceptually speaking. A language construct can be quite complex. The only thing different about language construction is that the compiler has direct science to it.

In general a cast operator or just informs the compiler that he knows what he is doing, in which case it is of course faster than the function, or does a conversion operation that is essentially the same as calling the function. Okay, the call might be slightly worse. But it may be the opposite, have to see the actual implementation of each. It may be that cast does more things or is poorly done.

Differences

Of course there are situations where the function may be better or the only option. A second parameter has already been mentioned indicating conversion basis.

A cast can not convert anything to string properly. The strval() function may be more successful, after all it tries to query the __toString() method for do the conversion. cast does not do this.

Obviously some contexts only accept functions. This occurs in functions that expect you to send a string with a function name to do callback (awful thing to do).

Some people find the function more readable than cast . Not me.

Apparently implicit type coercion is done with casting .

Performance

For all I read it seems that cast in PHP is always faster, at least slightly. So in theory it should be used preferentially. A test that I found was in the OS . Another complete test .

I did a similar test in some environments and got differences close to 50% for better in cast using PHP 7 and passing 200% in PHP 5.x.

The test was done with an algorithm that does a string conversion to int , it would not make sense to get an integer to convert to itself. I have run millions of times to measure the operation, not the preparation . The algorithm can be seen in PHP SandBox .

I saw Daniel Omine's test result. I did not see how it was done and results in 5.6. I saw that the result was 100 to 200 times slower than mine. Because it measures the interpretation of the code more than the operation itself.

That's what I always say, in many cases PHP will have spent significantly on the load and interpretation of the code and not so much on its execution itself. So performance in PHP is not so important, after all the mechanism is already very slow. In this example the preparation consumed at least 99% of the time.

I emphasize that I'm speculating a bit about the other test since it does not show how it was done. Then there may be deception about it.

Make your own test in the circumstances you are going to use and be aware of the changes in each version. What's worth today may not be worth tomorrow.

These tests tell me that the function is badly done and cast should be preferred, especially in PHP 5 where the difference is brutal.

Conclusion

That's what I've been looking for in the community, which I do not consider to be the most reliable. That may be so, but there may be something that no one has noticed. I will not delve into the sources of language to find out, I will trust. It does not seem to make a fundamental difference.

    
05.10.2016 / 02:50
2

Testing with casting

Summary: 40531158 * 10 -6 (4 millionths)

//
/*
O tempo tem uma variação
0.0000028610229
0.0000038146973
Normalmente entre 0.0000040531158 e 0.0000050067902
*/
$v = 42;
echo (int)$v;

Testing with function

Summary: 59604645 * 10 -6 (5.9 millionths)

//
/*
O tempo tem uma variação
0.0000109672546 (quando tem uma pausa de mais de 10 segundos entre uma execução e outra)

Normalmente entre 0.0000059604645 e 0.0000069604645
*/
$v = 42;
echo intval($v);

Note:

After testing several times, both had the same runtime of 5.9 millionths for some time.

Environment:

Macbook Pro 2011 (early)
CPU: Intel Core i7 2.2GHz
RAM: 4GB 1333 MHz DDR3

PHP: 7.0.10 (php-osx.liip.ch by Liip)
OS: El Capitan 10.11.6

* Testing with PHP 5.5.36, under the same environment, the results are identical.

    
05.10.2016 / 03:52