Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- define('TESTLEN', 5);
- define('TESTCOUNT', 1001); // Run 1000 times
- $s1 = "test"; // length 4, last index 3
- $s2 = "testX"; // length 5, last index 4
- include 'string.php';
- class String1 {
- protected $val;
- public function __construct($val) {
- $this->val = $val;
- }
- public function getLength() {
- return strlen($this->val);
- }
- }
- for ($x=0; $x < 6; $x++) {
- echo "\n\n====> Test run $x\n";
- $start = microtime(TRUE);
- for ($i=0; $i < TESTCOUNT; $i++) {
- $s = isset($s1{TESTLEN - 1}); // FALSE, NOTE: 0-based index
- $s = isset($s2{TESTLEN - 1}); // TRUE
- }
- $end = microtime(TRUE);
- echo "Test with {} = ".($end - $start)."\n";
- $start = microtime(TRUE);
- for ($i=0; $i < TESTCOUNT; $i++) {
- $s = strlen($s1) < TESTLEN; // FALSE
- $s = strlen($s2) < TESTLEN; // TRUE
- }
- $end = microtime(TRUE);
- echo "Test with strlen() = ".($end - $start)."\n";
- $start = microtime(TRUE);
- for ($i=0; $i < TESTCOUNT; $i++) {
- $s = mb_strlen($s1) < TESTLEN; // FALSE
- $s = mb_strlen($s2) < TESTLEN; // TRUE
- }
- $end = microtime(TRUE);
- echo "Test with mb_strlen() = ".($end - $start)."\n";
- $start = microtime(TRUE);
- for ($i=0; $i < TESTCOUNT; $i++) {
- $t = new String1($s1);
- $s = $t->getLength() < TESTLEN; // FALSE
- $t = new String1($s2);
- $s = $t->getLength() < TESTLEN; // TRUE
- }
- $end = microtime(TRUE);
- echo "Test with String1 object = ".($end - $start)."\n";
- $start = microtime(TRUE);
- for ($i=0; $i < TESTCOUNT; $i++) {
- $t = new String($s1);
- $s = $t->getLength() < TESTLEN; // FALSE
- $t = new String($s2);
- $s = $t->getLength() < TESTLEN; // TRUE
- }
- $end = microtime(TRUE);
- echo "Test with String object = ".($end - $start)."\n";
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement