Skip to content

Instantly share code, notes, and snippets.

@Sanaldev
Last active February 17, 2016 10:36
Show Gist options
  • Save Sanaldev/795ccf052d3a77b79a38 to your computer and use it in GitHub Desktop.
Save Sanaldev/795ccf052d3a77b79a38 to your computer and use it in GitHub Desktop.
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<!-- <form action="evalcode.php" id="usrform" method="post">-->
Code 1: <textarea id="code1" name="code1" rows="10" cols="70"></textarea>&nbsp;&nbsp;&nbsp; Code2: <textarea id="code2" name="code2" rows="10" cols="70"></textarea></br>
No of Iterations: <input type="text" name="itr" id="itr">&nbsp;</br></br>
<div align="center">
<input type="submit" align="center" onclick="callAjax()">
</div>
<!--</form>-->
</br></br>
<div id="result" style="display:block;"></div>
<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
<script>
function callAjax() {
var code1 = $("#code1").val();
var code2 = $("#code2").val();
var iteration = $("#itr").val();
$.ajax({type: "POST",
url: "evalcode.php",
data: { code1: code1, code2: code2 ,itr: iteration}}).done(function( msg ) {
//console.log( "Data Saved: " + msg );
$('#result').html(msg);
});
}
</script>
</body>
</html>
<?php
$code1 = @filter_var($_POST['code1']);
$code2 = @filter_var($_POST['code2']);
$iter = intval(@filter_var($_POST['itr']));
$tmpfname1 = tempnam("/tmp", "code");
$content1 = '<?php'.PHP_EOL.'function method1(){'.PHP_EOL.$code1.PHP_EOL.' }'.PHP_EOL.'?>';
file_put_contents($tmpfname1, $content1);
include $tmpfname1;
$tmpfname2 = tempnam("/tmp", "code");
$handle = fopen($tmpfname2, "w+");
$content2 = '<?php'.PHP_EOL.'function method2(){'.PHP_EOL.$code2.PHP_EOL.' }'.PHP_EOL.'?>';
fwrite($handle, $content2);
fclose($handle);
include $tmpfname2;
$start_time1 = microtime(true);
for($m=0;$m<$iter;$m++){
method1();
}
$end_time1 = microtime(true);
$start_time2 = microtime(true);
for($m=0;$m<$iter;$m++){
method2();
}
$end_time2 = microtime(true);
$exec_time1 = ($end_time1 - $start_time1);
$exec_time2 = ($end_time2 - $start_time2);
unlink($tmpfname1);
unlink($tmpfname2);
ob_clean();
header('Content-Type: text/html; charset=ISO-8859-1');
echo "</br>";
printf("<p>Code1 took -><b>%.3f</b>ms & Code2 -><b>%.3f</b>ms.</p>", $exec_time1, $exec_time2);
echo "</br>";
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment