How PHP converts strings to integers and performance test examples

Source: Internet
Author: User
In PHP, we can use 3 ways to convert a string to an integer.
1. Coercion type conversion Mode
Coercion type conversion is the way to "precede the variable to be converted with a target type enclosed in parentheses".

<?php $foo = "1"; $foo is the string type $bar = (int) $foo; $bar is an integral type?>

For an integer type, the cast type name is int or integer.
2. Built-in function mode
The built-in function is to use PHP's built-in function intval to perform variable conversion operations.

<?php $foo = "1"; $foo is the string type $bar = Intval ($foo); $bar is an integral type?>

The format of the Intval function is:
int intval (mixed $var [, int $base]);
Although the PHP manual explicitly states that Intval () cannot be used for the conversion of array and object. But after I tested it, there was no problem with converting an array, with a value of 1 instead of 0. I'm afraid because inside PHP, a variable of type array is considered non-0 worth it.

When you convert an object, PHP gives the following notice:
Object of class xxxx could not being converted to Int. xxxxx.php on line XX
The conversion value is also 1.
3. Formatting strings
Format the string by using sprintf%d to format the specified variable to achieve the purpose of type conversion.

<?php $foo = "1"; $foo is the string type $bar = sprintf ("%d", $foo); $bar is a string type?>

The sprintf is strictly a string type, so it should not be considered as a way to convert a string into an integer. But the string value after his processing has indeed become "an integer that is coerced into a string type."

For the average programmer, see this is the end, but for some perverted programmers, say the following performance test:

1. Basic function test
Set the following array:

<?php $a [] = "1"; $a [] = "A1"; $a [] = "1a"; $a [] = "1a2"; $a [] = "0"; $a [] = Array (' 4 ', 2); $a [] = "2.3"; $a [] = "1"; $a [] = new Directory ();?>

Use three different ways to convert the elements in the array given above to see how the conversion is happening. The program source code is as follows:

<?php $a [] = "1"; $a [] = "A1"; $a [] = "1a"; $a [] = "1a2"; $a [] = "0"; $a [] = Array (' 4 ', 2); $a [] = "2.3"; $a [] = "1"; $a [] = new Directory (); int print "(int) <br/>"; foreach ($a as $v) {var_dump ((int) $v); print "<br/>";}//Intval print "intval (); <br/>"; foreach ($a as $v) {Var_dump (Intval ($v)); print "<br/>";}//sprintf print "sprintf (); <br/>"; foreach ($a as $v) {Var_dump (sprintf ("%d", $v)), print "<br/>";}?>

The final running result of the program is as follows (the notice that appears when converting object has been removed):

(int) int (1) int (0) int (1) int (1) int (0) int (1) int (2) int ( -1) int (1) intval (); int (1) int (0) int (1) int (1) int (0) int (1) int (2) int ( -1) int (1) sprintf ();  String (1) "1" string (1) "0" string (1) "1" string (1) "1" string (1) "0" string (1) "1" string (1) "2" string (2) "-1" string "1"

As you can see, the results of the three conversions are exactly the same. Then functionally speaking, 3 ways can be competent for conversion work, then the next task is to see which one is more efficient.

2. Performance Testing

<?php $foo = "1"; SELECT * ... ";?> gets the point-in-time function as follows (used to get the test start and end points to calculate the consumption time): <?php * * Simple function to replicate PHP 5 behaviour */FU Nction microtime_float () {list ($usec, $sec) = Explode ("", Microtime ()); return (float) $usec + (float) $sec); }?>

The test process is to convert the variable $foo 1 million times (1 million times) using each method, and output the respective consumption time in a total of three sets of tests to minimize the error. The test procedure is as follows:

<?php function Microtime_float () {list ($usec, $sec) = Explode ("", Microtime ()); return (float) $usec + (float) $sec); } $foo = "1"; SELECT * ... "; (int) $fStart = Microtime_float (); for ($i =0; $i <1000000; $i + +) {$bar = (int) $foo;} $fEnd = Microtime_float (); print "(int):". ($fEnd-$fStart). "S<br/>"; Intval () $fStart = Microtime_float (); for ($i =0; $i <1000000; $i + +) {$bar = Intval ($foo);} $fEnd = Microtime_float (); Print "Intval ():". ($fEnd-$fStart). "S<br/>"; sprintf () $fStart = Microtime_float (); for ($i =0; $i <1000000; $i + +) {$bar = sprintf ("%d", $foo);} $fEnd = Microtime_float (); Print "sprintf ():". ($fEnd-$fStart). "S<br/>";?>

The final Test result:

(int): 0.67205619812012s intval (): 1.1603000164032s sprintf (): 2.1068270206451s (int): 0.66051411628723s intval () : 1.1493890285492s sprintf (): 2.1008238792419s (int): 0.66878795623779s intval (): 1.1613430976868s sprintf () : 2.0976209640503s

As you can see, converting a string to an integer speed using a forced type conversion is the fastest.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.