Advertisement
Guest User

string length test

a guest
Dec 12th, 2010
402
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.88 KB | None | 0 0
  1. <?php
  2.  
  3. define('TESTLEN', 5);
  4. define('TESTCOUNT', 1001); // Run 1000 times
  5. $s1 = "test"; // length 4, last index 3
  6. $s2 = "testX"; // length 5, last index 4
  7.  
  8. include 'string.php';
  9.  
  10. class String1 {
  11.     protected $val;
  12.     public function __construct($val) {
  13.         $this->val = $val;
  14.     }
  15.     public function getLength() {
  16.         return strlen($this->val);
  17.     }
  18. }
  19.  
  20.  
  21. for ($x=0; $x < 6; $x++) {
  22.     echo "\n\n====> Test run $x\n";
  23.     $start = microtime(TRUE);
  24.     for ($i=0; $i < TESTCOUNT; $i++) {
  25.         $s = isset($s1{TESTLEN - 1}); // FALSE, NOTE: 0-based index
  26.         $s = isset($s2{TESTLEN - 1}); // TRUE
  27.     }
  28.     $end = microtime(TRUE);
  29.     echo "Test with {} = ".($end - $start)."\n";
  30.  
  31.     $start = microtime(TRUE);
  32.     for ($i=0; $i < TESTCOUNT; $i++) {
  33.         $s = strlen($s1) < TESTLEN; // FALSE
  34.         $s = strlen($s2) < TESTLEN; // TRUE
  35.     }
  36.     $end = microtime(TRUE);
  37.     echo "Test with strlen() = ".($end - $start)."\n";
  38.  
  39.     $start = microtime(TRUE);
  40.     for ($i=0; $i < TESTCOUNT; $i++) {
  41.         $s = mb_strlen($s1) < TESTLEN; // FALSE
  42.         $s = mb_strlen($s2) < TESTLEN; // TRUE
  43.     }
  44.     $end = microtime(TRUE);
  45.     echo "Test with mb_strlen() = ".($end - $start)."\n";
  46.  
  47.  
  48.     $start = microtime(TRUE);
  49.     for ($i=0; $i < TESTCOUNT; $i++) {
  50.         $t = new String1($s1);
  51.         $s = $t->getLength() < TESTLEN; // FALSE
  52.         $t = new String1($s2);
  53.         $s = $t->getLength() < TESTLEN; // TRUE
  54.     }
  55.     $end = microtime(TRUE);
  56.     echo "Test with String1 object = ".($end - $start)."\n";
  57.  
  58.     $start = microtime(TRUE);
  59.     for ($i=0; $i < TESTCOUNT; $i++) {
  60.         $t = new String($s1);
  61.         $s = $t->getLength() < TESTLEN; // FALSE
  62.         $t = new String($s2);
  63.         $s = $t->getLength() < TESTLEN; // TRUE
  64.     }
  65.     $end = microtime(TRUE);
  66.     echo "Test with String object = ".($end - $start)."\n";
  67.  
  68. }
  69.  
  70. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement