Page 2 of 2

Posted: Mon Mar 28, 2005 5:39 pm
by feyd
uh huh... did you notice that if $password{4} does exist, you get a notice?

Code: Select all

Notice: Uninitialized string offset: 4 in /path/to/your/file.php on line 2
This is pretty interesting though:

Code: Select all

<?php

$iterations = array(1,1000000);
$test = 'this is a string of 33 characters';
$offset = 12;

function testMe($it, $offset)
{
	$s1 = microtime();
	for($i = 0; $i < $it; $i++)
	{
		$v = (strlen($test) > 35);
	}
	$e1 = microtime();

	$s1 = explode(' ', $s1);
	$e1 = explode(' ', $e1);

	$s2 = microtime();
	for($i = 0; $i < $it; $i++)
	{
		$v = ($test{$offset} != '');
	}
	$e2 = microtime();

	$s2 = explode(' ', $s2);
	$e2 = explode(' ', $e2);

	echo 'function test' . &quote;\n&quote;;
	echo number_format($it) . ' iterations.' . &quote;\n&quote;;
	echo 'strlen($test) took ' . ($e1ї1] - $s1ї1] + $e1ї0] - $s1ї0]) . ' seconds  (averaging ' . (($e1ї1] - $s1ї1] + $e1ї0] - $s1ї0]) / $it) . ' seconds per iteration)' . &quote;\n&quote;;
	echo '    $test{' . $offset . '} took ' . ($e2ї1] - $s2ї1] + $e2ї0] - $s2ї0]) . ' seconds  (averaging ' . (($e2ї1] - $s2ї1] + $e2ї0] - $s2ї0]) / $it) . ' seconds per iteration)' . &quote;\n\n\n&quote;;
}

foreach($iterations as $it)
{
	$s1 = microtime();
	for($i = 0; $i < $it; $i++)
	{
		$v = (strlen($test) > 35);
	}
	$e1 = microtime();

	$s1 = explode(' ', $s1);
	$e1 = explode(' ', $e1);

	$s2 = microtime();
	for($i = 0; $i < $it; $i++)
	{
		$v = ($test{$offset} != '');
	}
	$e2 = microtime();

	$s2 = explode(' ', $s2);
	$e2 = explode(' ', $e2);

	echo 'inline test' . &quote;\n&quote;;
	echo number_format($it) . ' iterations.' . &quote;\n&quote;;
	echo 'strlen($test) took ' . ($e1ї1] - $s1ї1] + $e1ї0] - $s1ї0]) . ' seconds  (averaging ' . (($e1ї1] - $s1ї1] + $e1ї0] - $s1ї0]) / $it) . ' seconds per iteration)' . &quote;\n&quote;;
	echo '    $test{' . $offset . '} took ' . ($e2ї1] - $s2ї1] + $e2ї0] - $s2ї0]) . ' seconds  (averaging ' . (($e2ї1] - $s2ї1] + $e2ї0] - $s2ї0]) / $it) . ' seconds per iteration)' . &quote;\n\n\n&quote;;
	
	
	testMe($it, $offset);
}

?>

Code: Select all

inline test
1 iterations.
strlen($test) took 3.5000000000007E-005 seconds  (averaging 3.5000000000007E-005 seconds per iteration)
    $test{12} took 1.6999999999934E-005 seconds  (averaging 1.6999999999934E-005 seconds per iteration)


function test
1 iterations.
strlen($test) took 4.4000000000044E-005 seconds  (averaging 4.4000000000044E-005 seconds per iteration)
    $test{12} took 1.3999999999958E-005 seconds  (averaging 1.3999999999958E-005 seconds per iteration)


inline test
1,000,000 iterations.
strlen($test) took 1.266645 seconds  (averaging 1.266645E-006 seconds per iteration)
    $test{12} took 1.785079 seconds  (averaging 1.785079E-006 seconds per iteration)


function test
1,000,000 iterations.
strlen($test) took 2.674093 seconds  (averaging 2.674093E-006 seconds per iteration)
    $test{12} took 2.267316 seconds  (averaging 2.267316E-006 seconds per iteration)

Personally, I'd use strlen(), as the logic will make more sense to someone who is not familiar with the code.

Posted: Mon Mar 28, 2005 5:42 pm
by Ambush Commander
uh huh... did you notice that if $password{4} does exist, you get a notice?
Erp. Hmm... must be E_NOTICE. Well, scratch that idea.

EDIT | Actually, I think you may have to use isset() or some other thing that doesn't throw an error when something returns null. Maybe it's E_WARNING. Hmm
Personally, I'd use strlen(), as the logic will make more sense to someone who is not familiar with the code.
True, true.

Posted: Mon Mar 28, 2005 5:50 pm
by feyd
:oops: my code had a small bug in it. Updated:

Code: Select all

<?php

$iterations = array(1,1000000);
$test = 'this is a string of 33 characters';
$offset = 12;

function testMe($it, $test, $offset)
{
	$s1 = microtime();
	for($i = 0; $i < $it; $i++)
	{
		$v = (strlen($test) > 35);
	}
	$e1 = microtime();

	$s1 = explode(' ', $s1);
	$e1 = explode(' ', $e1);

	$s2 = microtime();
	for($i = 0; $i < $it; $i++)
	{
		$v = ($test{$offset} != '');
	}
	$e2 = microtime();

	$s2 = explode(' ', $s2);
	$e2 = explode(' ', $e2);

	echo 'function test' . &quote;\n&quote;;
	echo number_format($it) . ' iterations.' . &quote;\n&quote;;
	echo 'strlen($test) took ' . ($e1ї1] - $s1ї1] + $e1ї0] - $s1ї0]) . ' seconds  (averaging ' . (($e1ї1] - $s1ї1] + $e1ї0] - $s1ї0]) / $it) . ' seconds per iteration)' . &quote;\n&quote;;
	echo '    $test{' . $offset . '} took ' . ($e2ї1] - $s2ї1] + $e2ї0] - $s2ї0]) . ' seconds  (averaging ' . (($e2ї1] - $s2ї1] + $e2ї0] - $s2ї0]) / $it) . ' seconds per iteration)' . &quote;\n\n\n&quote;;
}

foreach($iterations as $it)
{
	$s1 = microtime();
	for($i = 0; $i < $it; $i++)
	{
		$v = (strlen($test) > 35);
	}
	$e1 = microtime();

	$s1 = explode(' ', $s1);
	$e1 = explode(' ', $e1);

	$s2 = microtime();
	for($i = 0; $i < $it; $i++)
	{
		$v = ($test{$offset} != '');
	}
	$e2 = microtime();

	$s2 = explode(' ', $s2);
	$e2 = explode(' ', $e2);

	echo 'inline test' . &quote;\n&quote;;
	echo number_format($it) . ' iterations.' . &quote;\n&quote;;
	echo 'strlen($test) took ' . ($e1ї1] - $s1ї1] + $e1ї0] - $s1ї0]) . ' seconds  (averaging ' . (($e1ї1] - $s1ї1] + $e1ї0] - $s1ї0]) / $it) . ' seconds per iteration)' . &quote;\n&quote;;
	echo '    $test{' . $offset . '} took ' . ($e2ї1] - $s2ї1] + $e2ї0] - $s2ї0]) . ' seconds  (averaging ' . (($e2ї1] - $s2ї1] + $e2ї0] - $s2ї0]) / $it) . ' seconds per iteration)' . &quote;\n\n\n&quote;;
	
	
	testMe($it, $test, $offset);
}

?>

Code: Select all

inline test
1 iterations.
strlen($test) took 3.599999999998E-005 seconds  (averaging 3.599999999998E-005 seconds per iteration)
    $test{12} took 4.2999999999987E-005 seconds  (averaging 4.2999999999987E-005 seconds per iteration)


function test
1 iterations.
strlen($test) took 2.1999999999994E-005 seconds  (averaging 2.1999999999994E-005 seconds per iteration)
    $test{12} took 1.5000000000015E-005 seconds  (averaging 1.5000000000015E-005 seconds per iteration)


inline test
1,000,000 iterations.
strlen($test) took 1.293642 seconds  (averaging 1.293642E-006 seconds per iteration)
    $test{12} took 1.777034 seconds  (averaging 1.777034E-006 seconds per iteration)


function test
1,000,000 iterations.
strlen($test) took 1.263796 seconds  (averaging 1.263796E-006 seconds per iteration)
    $test{12} took 1.673452 seconds  (averaging 1.673452E-006 seconds per iteration)
switching to isset() doesn't affect the high iteration test much, but does make the single test run three times faster.

Posted: Mon Mar 28, 2005 5:54 pm
by Ambush Commander
Well then, scratch that! strlen() is better, for logical and theoretical and common-sense reasons.

Posted: Mon Mar 28, 2005 8:09 pm
by anthony88guy
ok, thxs guys