Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Run a test. </p> <pre><code> string coerce: 7.42296099663 string cast: 8.05654597282 string fail coerce: 7.14159703255 string fail cast: 7.87444186211 </code></pre> <p>This was a test that ran each scenario 10,000,000 times. :-)</p> <p>Co-ercion is <code>0 + "123"</code></p> <p>Casting is <code>(integer)"123"</code></p> <p>I think Co-ercion is a tiny bit faster. Oh, and trying <code>0 + array('123')</code> is a fatal error in PHP. You might want your code to check the type of the supplied value.</p> <p>My test code is below.</p> <hr> <pre><code>function test_string_coerce($s) { return 0 + $s; } function test_string_cast($s) { return (integer)$s; } $iter = 10000000; print "-- running each text $iter times.\n"; // string co-erce $string_coerce = new Timer; $string_coerce-&gt;Start(); print "String Coerce test\n"; for( $i = 0; $i &lt; $iter ; $i++ ) { test_string_coerce('123'); } $string_coerce-&gt;Stop(); // string cast $string_cast = new Timer; $string_cast-&gt;Start(); print "String Cast test\n"; for( $i = 0; $i &lt; $iter ; $i++ ) { test_string_cast('123'); } $string_cast-&gt;Stop(); // string co-erce fail. $string_coerce_fail = new Timer; $string_coerce_fail-&gt;Start(); print "String Coerce fail test\n"; for( $i = 0; $i &lt; $iter ; $i++ ) { test_string_coerce('hello'); } $string_coerce_fail-&gt;Stop(); // string cast fail $string_cast_fail = new Timer; $string_cast_fail-&gt;Start(); print "String Cast fail test\n"; for( $i = 0; $i &lt; $iter ; $i++ ) { test_string_cast('hello'); } $string_cast_fail-&gt;Stop(); // ----------------- print "\n"; print "string coerce: ".$string_coerce-&gt;Elapsed()."\n"; print "string cast: ".$string_cast-&gt;Elapsed()."\n"; print "string fail coerce: ".$string_coerce_fail-&gt;Elapsed()."\n"; print "string fail cast: ".$string_cast_fail-&gt;Elapsed()."\n"; class Timer { var $ticking = null; var $started_at = false; var $elapsed = 0; function Timer() { $this-&gt;ticking = null; } function Start() { $this-&gt;ticking = true; $this-&gt;started_at = microtime(TRUE); } function Stop() { if( $this-&gt;ticking ) $this-&gt;elapsed = microtime(TRUE) - $this-&gt;started_at; $this-&gt;ticking = false; } function Elapsed() { switch( $this-&gt;ticking ) { case true: return "Still Running"; case false: return $this-&gt;elapsed; case null: return "Not Started"; } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload