<?php

/*
Skrol29, 2012-02-10
http://www.tinybutstrong.com/onlyyou.html
*/
f_InfoStart('instruction global vs array $GLOBALS');
f_EchoLine('Presentation', 'u');
echo "This test compares two ways of accessing global variables.<br />";
/* --------------
   Speed measures
   -------------- */
f_EchoLine();
f_EchoLine('Speed measures', 'u');
$b0 = f_BenchThisFct('f_Nothing');
// isset() returning false
$isset1_f = f_BenchThisFct('f_test_global_isset');
$isset2_f = f_BenchThisFct('f_test_globals_isset');
$MyVariable = array();
$MyVariable['item1'] = "Value 1";
$MyVariable['item2'] = "Value 2";
$MyVariable['item3'] = "Value 3";
// isset() returning true
$isset1_t = f_BenchThisFct('f_test_global_isset');
$isset2_t = f_BenchThisFct('f_test_globals_isset');
// get value
$getval1 = f_BenchThisFct('f_test_global_getval');
$getval2 = f_BenchThisFct('f_test_globals_getval');
// get item
$item = array('item1');
$getitem1 = f_BenchThisFct('f_test_global_getitem', $item);
<?php

/*
Skrol29, 2010-12-16
http://www.tinybutstrong.com/onlyyou.html
*/
f_InfoStart('Reverse string search (backwards)');
f_EchoLine('Presentation', 'u');
echo "\r\nThis test compares several ways for a reverse search in a string, starting at a given offset.<br />\r\nReverse search is not easy in PHP for two reasons: <br />\r\n- the native function strrpos(\$haystack,\$needle,\$offset) does not start a reverse search from the offset, but gives the last needle in the hastack starting from the offset.<br />\r\n- in PHP 4, the needle can be only one character.<br />\r\n<br />\r\nMethods tested:\r\n<table border='0' padding='3'>\r\n <tr><td>StrrposCut:            </td><td>cut the string and use strrpos()</td></tr>\r\n <tr><td>StrposUntilOffset:     </td><td>use strpos() to found all locations from the start until the offset, return the last one</td></tr>\r\n <tr><td>ReverseFullStr:        </td><td>reverse both strings and use strpos()</td></tr>\r\n <tr><td>ReverseCutStr:         </td><td>cut the haystack string, revserse strings and use strpos()</td></tr>\r\n <tr><td>ByChar_CharAsArray:    </td><td>read characters one by one backwards from the offset, use \$chr=\$txt[\$i]</td></tr>\r\n <tr><td>ByChar_CharAsSubstr:   </td><td>read characters one by one backwards from the offset, use \$chr=substr(\$txt,\$i,1)</td></tr>\r\n <tr><td>ByChar_NoCharPrecheck: </td><td>read characters one by one backwards from the offset, use \$chr=substr(\$txt,\$i,1), but do not check first character. </td></tr>\r\n</table>";
/* ------------
   initilize data and check the result of functions
   ------------ */
f_EchoLine();
f_EchoLine('Initialize data', 'u');
$txt = f_GetHtml();
$what = '<div';
$offset = strpos($txt, 'super_conforming_strrpos');
$res_good = 43338;
f_EchoLine("Length of string: " . strlen($txt));
f_EchoLine("Item seached: '" . $what . "'");
f_EchoLine("Offset for the search = " . $offset . " , extract: '" . f_GiveExample($txt, $offset, 50) . "'...");
f_EchoLine("Expected result: " . $res_good . " , extract: '" . f_GiveExample($txt, $res_good, 50) . "'...");
f_EchoLine();
f_EchoLine('Check results of functions', 'u');
$res_StrrposCut = f_revsrch_StrrposCut($txt, $what, $offset);
$res_StrposUntilOffset = f_revsrch_StrposUntilOffset($txt, $what, $offset);
$res_ReverseFullStr = f_revsrch_ReverseFullStr($txt, $what, $offset);
$res_ReverseCutStr = f_revsrch_ReverseCutStr($txt, $what, $offset);
$res_ByChar_CharAsArray = f_revsrch_ByChar_CharAsArray($txt, $what, $offset);
$res_ByChar_CharAsSubstr = f_revsrch_ByChar_CharAsSubstr($txt, $what, $offset);
$res_ByChar_NoCharPrecheck = f_revsrch_ByChar_NoCharPrecheck($txt, $what, $offset);
<?php

/*
Skrol29, 2010-12-16
http://www.tinybutstrong.com/onlyyou.html
*/
f_InfoStart('Function vs Method');
f_EchoLine('Presentation', 'u');
echo "\r\nThis test compares the time execution between:<br />\r\n- a function<br />\r\n- a normal method of an instanciated object<br />\r\n- a static method of a class<br />\r\n- a static method called as a normal method<br />";
/* --------------
   Speed measures
   -------------- */
f_EchoLine();
f_EchoLine('Speed measures', 'u');
$obj = new clsTest();
$x = 29;
$prm = array(&$obj, $x);
$b0 = f_BenchThisFct('f_Nothing');
$b_Function = f_BenchThisFct('f_function_normal', $prm);
$b_NormalMethod = f_BenchThisFct('f_method_normal', $prm);
$b_StaticMethod = f_BenchThisFct('f_method_static', $prm);
$b_StaticMethodAsNormal = f_BenchThisFct('f_method_static_as_normal', $prm);
/* ---------------
   compare results
   --------------- */
f_EchoLine();
f_EchoLine('Compare results', 'u');
f_Compare("Function", $b_Function, "NormalMethod", $b_NormalMethod);
f_Compare("Function", $b_Function, "StaticMethod", $b_StaticMethod);
f_Compare("Function", $b_Function, "StaticMethodAsNormal", $b_StaticMethodAsNormal);
f_Compare("Method", $b_NormalMethod, "StaticMethod", $b_StaticMethod);
<?php

/*
Skrol29, 2010-12-16
http://www.tinybutstrong.com/onlyyou.html
*/
set_time_limit(0);
f_InfoStart('Arrays vs Objects');
f_EchoLine('Presentation', 'u');
echo "This test compares the usage of 4 information storage:\r\n<table border='0' padding='3'>\r\n <tr><td>array:           </td><td>&nbsp;</td><td>\$x = array('name' => 'James', 'subname' => 'Dean', 'id' => 33);</td></tr>\r\n <tr><td>new instance of object: </td><td>&nbsp;</td><td>\$x = new clsTest; class clsTest {var \$name = 'James'; var \$subname = 'Dean'; var \$id = 33;}</td></tr>\r\n <tr><td>array converted in object (stdClass): </td><td>&nbsp;</td><td>\$x = (object) array('name' => 'James', 'subname' => 'Dean', 'id' => 33);</td></tr>\r\n <tr><td>named variables: </td><td>&nbsp;</td><td>\$name = 'James'; \$subname = 'Dean'; \$id = 33;</td></tr>\r\n</table>";
/* ------------
   memory measures
   ------------ */
f_EchoLine();
f_EchoLine('Memory measures', 'u');
$mem0 = (int) 0;
$mem1 = (int) 0;
$a = array('name' => 'James', 'subname' => 'Dean', 'id' => 33);
$i = new clsTest();
$o = (object) array('name' => 'James', 'subname' => 'Dean', 'id' => 33);
$name = 'James';
$subname = 'Dean';
$id = 33;
$mem0 = memory_get_usage();
unset($a);
$mem1 = memory_get_usage();
f_EchoLine("Memory size for the array: " . ($mem0 - $mem1) . " bytes");
$mem0 = memory_get_usage();
unset($i);
$mem1 = memory_get_usage();
f_EchoLine("Memory size for the specific object: " . ($mem0 - $mem1) . " bytes");
<?php

/*
Skrol29, 2010-12-16
http://www.tinybutstrong.com/onlyyou.html
*/
f_InfoStart('file_existe() vs @fopen()');
f_EchoLine('Presentation', 'u');
echo "This test compares two ways of opening a file that may or may not exist.<br />";
/* --------------
   Speed measures
   -------------- */
f_EchoLine();
f_EchoLine('Speed measures', 'u');
$b0 = f_BenchThisFct('f_Nothing');
$prm_ok = array(basename(__FILE__));
$prm_err = array('this_file_do_not_exists.txt');
$b_fe_ok = f_BenchThisFct('f_test_file_exists', $prm_ok);
$b_fe_err = f_BenchThisFct('f_test_file_exists', $prm_err);
$b_of_ok = f_BenchThisFct('f_test_ofile', $prm_ok);
$b_of_err = f_BenchThisFct('f_test_ofile', $prm_err);
/* ---------------
   compare results
   --------------- */
f_EchoLine();
f_EchoLine('Compare results', 'u');
f_Compare("file_exists() with existing file", $b_fe_ok, "@fopen() with existing file", $b_of_ok);
f_Compare("file_exists() with non existing file", $b_fe_err, "@fopen() with non existing file", $b_of_err);
/* ------------
   end
   ------------ */