Jump to content
Sign in to follow this  
terryds

Please help me fix my benchmark snippet

Recommended Posts

I have a php benchmarking function, but it doesn't work as expected.

<?php function speedster($arg, $name="") {static $time_start; if($arg == 'start') {echo "<p>The time consumed by $name method is ";$time_start = microtime(true);} elseif ($arg == 'end') {$time_end = microtime(true);$time_ex = $time_end - $time_start;printf("%.9f seconds.</p>", $time_ex);}else {die('Invalid argument');}}

This is how i use the code :

 

<?php

include 'function.speedster.php';

$password = 'passwordku';
speedster('start','isset()');
if(isset($password[4]));
speedster('end');
speedster('start','strlen()');
if(strlen($password) > 5);
speedster('end');
And, the result is very different from the one without function (which always tells that isset() is faster than strlen())
<?php
$password = 'passwordku';
$timestart = microtime(true);
if(isset($password[4]));
$timeend = microtime(true);
$totaltime = $timeend-$timestart;
printf("<p>The time consumed by isset() method is %.9f seconds.</p>", $totaltime) ;
$timestart = microtime(true);
if(strlen($password) > 5);
$timeend = microtime(true);
$totaltime = $timeend-$timestart;
printf("<p>The time consumed by strlen() method is %.9f seconds.</p>", $totaltime) ;
Edited by terryds

Share this post


Link to post
Share on other sites

What's it not doing?

Share this post


Link to post
Share on other sites

The major difference is the overhead from the function call. When you are benchmarking things that take very little time, it is typically more useful to run them in a loop. Have a loop where you run isset 10,000 times, and another loop where you run strlen 10,000 times, and compare those. A larger sample size will weed out small changes and show more of an average. Just make sure you use the exact same structure other than what you actually want to test, i.e. don't use a for loop to run one test, and a foreach or while loop to run the other.

  • Like 1

Share this post


Link to post
Share on other sites

Thanks for the advice, justsomeguy.

Now, I have rewritten the code and it tells me an error (parse error)

 

This is my function

 

<?php
function speedster($code="", $repetition=1, $name="this") {
$eval_start = microtime(true);
for ($i=0; $i < $repetition; $i++) {
eval("");
}
$eval_time = microtime(true) - $eval_start;
$time_start = microtime(true);
for ($i=0; $i < $repetition; $i++) {
eval($code);
}
$time_end = microtime(true);
$total_time = $time_end - $time_start - $eval_time;
printf("<p>The time consumed by $name method is %.9f seconds.</p>", $total_time);
}
This is how i use it
<?php
include 'function.speedster.php';
$username = "myusername";
// Using strlen()
$code = "if(strlen($username) > 5);";
speedster($code, 10, 'strlen()');
// Using isset()
$code = "if(isset($username[4]));";
speedster($code, 10, 'isset()');
And, it gives me this error :
Notice: Use of undefined constant myusername - assumed 'myusername'
Then, I changed the username variable into "'myusername'"
But, it doesn't solve the problem and the error occurs.
Parse error: syntax error, unexpected ')', expecting :: (T_PAAMAYIM_NEKUDOTAYIM) in C:xampphtdocsterrytesttestersfunction.speedster.php(13) : eval()'d code on line 1
Please help me fix my function.

 

Share this post


Link to post
Share on other sites

When you use double-quoted strings PHP fills in the values of variables. So when you do this:$code = "if(strlen($username) > 5);";Then PHP fills in the value of $username, and $code is set to this:

if(strlen(myusername) > 5);
That value doesn't have quotes around it, which is why it thinks you're trying to use a constant. You can put single quotes around that variable so that the value will be quoted in $code. Print out $code so that you can verify that it is valid PHP code that will work in an eval statement.

Share this post


Link to post
Share on other sites

I have changed the username variable into "'myusername'" (single quoted in double quote)But, it doesn't solve the problem and the error occurs.Parse error: syntax error, unexpected ')', expecting :: (T_PAAMAYIM_NEKUDOTAYIM) in C:xampphtdocsterrytesttestersfunction.speedster.php(13) : eval()'d code on line 1Printed out code : if(isset(s));Please help me fix my function.

Edited by terryds

Share this post


Link to post
Share on other sites
if(isset(s));
That code assumes that s is a constant. You're checking if the constant s is set.Your problem is that you are not sending all of the code you need to the function. Use single quotes to surround the code you want to send, and write all of the code there. That means variable definitions, function calls, etc. All of it goes in the string. Like I said, when you print that string it should contain valid code that PHP can run in any context.

Share this post


Link to post
Share on other sites

I changed the isset section into

// Using isset()
$code = "if(isset('$username[4]'));";
speedster($code, 10, 'isset()');
Then, I print out the code
if(isset('s'));
But, I don't know why error still occurs.
Output :
if(strlen('myusername') > 5);

The time consumed by strlen() method is 0.000248194 seconds.

if(isset('s'));

( ! ) Parse error: syntax error, unexpected ''s'' (T_CONSTANT_ENCAPSED_STRING) in C:xampphtdocsterrytesttestersfunction.speedster.php(14) : eval()'d code on line 1 Call Stack # Time Memory Function Location 1 0.0006 135416 {main}( ) ..strlen()_vs_isset().php:0 2 0.0014 140704 speedster( ) ..strlen()_vs_isset().php:11( ! ) Parse error: syntax error, unexpected quoted-string (T_CONSTANT_ENCAPSED_STRING) in C:xampphtdocsterrytesttestersfunction.speedster.php(14) : eval()'d code on line 1 Call Stack # Time Memory Function Location 1 0.0006 135416 {main}( ) ..strlen()_vs_isset().php:0 2 0.0014 140704 speedster( ) ..strlen()_vs_isset().php:11( ! ) Parse error: syntax error, unexpected quoted-string (T_CONSTANT_ENCAPSED_STRING) in C:xampphtdocsterrytesttestersfunction.speedster.php(14) : eval()'d code on line 1 Call Stack # Time Memory Function Location 1 0.0006 135416 {main}( ) ..strlen()_vs_isset().php:0 2 0.0014 140704 speedster( ) ..strlen()_vs_isset().php:11( ! ) Parse error: syntax error, unexpected quoted-string (T_CONSTANT_ENCAPSED_STRING) in C:xampphtdocsterrytesttestersfunction.speedster.php(14) : eval()'d code on line 1 Call Stack # Time Memory Function Location 1 0.0006 135416 {main}( ) ..strlen()_vs_isset().php:0 2 0.0014 140704 speedster( ) ..strlen()_vs_isset().php:11( ! ) Parse error: syntax error, unexpected quoted-string (T_CONSTANT_ENCAPSED_STRING) in C:xampphtdocsterrytesttestersfunction.speedster.php(14) : eval()'d code on line 1 Call Stack # Time Memory Function Location 1 0.0006 135416 {main}( ) ..strlen()_vs_isset().php:0 2 0.0014 140704 speedster( ) ..strlen()_vs_isset().php:11( ! ) Parse error: syntax error, unexpected quoted-string (T_CONSTANT_ENCAPSED_STRING) in C:xampphtdocsterrytesttestersfunction.speedster.php(14) : eval()'d code on line 1 Call Stack # Time Memory Function Location 1 0.0006 135416 {main}( ) ..strlen()_vs_isset().php:0 2 0.0014 140704 speedster( ) ..strlen()_vs_isset().php:11( ! ) Parse error: syntax error, unexpected quoted-string (T_CONSTANT_ENCAPSED_STRING) in C:xampphtdocsterrytesttestersfunction.speedster.php(14) : eval()'d code on line 1 Call Stack # Time Memory Function Location 1 0.0006 135416 {main}( ) ..strlen()_vs_isset().php:0 2 0.0014 140704 speedster( ) ..strlen()_vs_isset().php:11( ! ) Parse error: syntax error, unexpected quoted-string (T_CONSTANT_ENCAPSED_STRING) in C:xampphtdocsterrytesttestersfunction.speedster.php(14) : eval()'d code on line 1 Call Stack # Time Memory Function Location 1 0.0006 135416 {main}( ) ..strlen()_vs_isset().php:0 2 0.0014 140704 speedster( ) ..strlen()_vs_isset().php:11( ! ) Parse error: syntax error, unexpected quoted-string (T_CONSTANT_ENCAPSED_STRING) in C:xampphtdocsterrytesttestersfunction.speedster.php(14) : eval()'d code on line 1 Call Stack # Time Memory Function Location 1 0.0006 135416 {main}( ) ..strlen()_vs_isset().php:0 2 0.0014 140704 speedster( ) ..strlen()_vs_isset().php:11( ! ) Parse error: syntax error, unexpected quoted-string (T_CONSTANT_ENCAPSED_STRING) in C:xampphtdocsterrytesttestersfunction.speedster.php(14) : eval()'d code on line 1 Call Stack # Time Memory Function Location 1 0.0006 135416 {main}( ) ..strlen()_vs_isset().php:0 2 0.0014 140704 speedster( ) ..strlen()_vs_isset().php:11

The time consumed by isset() method is 0.015677929 seconds.

Please help me solve this error

Edited by terryds

Share this post


Link to post
Share on other sites

Look at the documentation for isset, especially the notes.http://www.php.net/manual/en/function.isset.phpisset('s') is not valid PHP code. You don't pass literal values to isset. Moreover, isset('s') is not what you are trying to benchmark, is it? Show the line of code that you want to execute in the benchmark.

Share this post


Link to post
Share on other sites

I just want to check which one is faster (isset or strlen)

 

So , I use this code

<?php
include 'function.speedster.php';
$username = "myusername";
// Using strlen()
$code = "if(strlen($username) > 5);"; // This line is what i want to benchmark
speedster($code, 10, 'strlen()');
// Using isset()
$code = "if(isset($username[4]));"; // This line is what i want to benchmark
speedster($code, 10, 'isset()');
Could you please show me the way to benchmark the speed ?
Edited by terryds

Share this post


Link to post
Share on other sites

but they don't do the same thing, so I don't get why you are comparing the two?

Share this post


Link to post
Share on other sites

I keep telling you that the code to eval needs to be complete, valid PHP code. You don't seem to understand that. Consider the difference:

$username = "myusername";$code = "if(isset($username[4]));";echo 'code1: ' . $code . '<br>';$code = '$username="myusername"; if(isset($username[4]));';echo 'code2: ' . $code . '<br>';
  • Like 1

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×
×
  • Create New...