Advertisement
Guest User

benchmarks for conditional existance

a guest
Jun 22nd, 2011
853
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.63 KB | None | 0 0
  1. <pre><?php
  2.  
  3. $c = mysql_connect('localhost', 'root', '');
  4.  
  5. if(!$c) die('!');
  6. echo mysql_select_db('test', $c)?'Connection':'Failure';
  7. echo PHP_EOL;
  8.  
  9. echo ':::::::::::::::::::::::::BEGINNING NON-EXISTING TABLE::::::::::::::::::::::::::::::';
  10. $start = microtime(1);
  11. for( $i = 0; $i < 100000; $i++ )
  12. {
  13.     mysql_query( 'SELECT 1 FROM foo' );
  14. }
  15. $end = microtime(1);
  16.  
  17. echo  ($end - $start) . " for bad select";
  18. echo PHP_EOL;
  19.  
  20. $start = microtime(1);
  21. for( $i = 0; $i < 100000; $i++ )
  22. {
  23.     $q = mysql_query("SELECT table_name FROM information_schema.tables WHERE table_schema = 'test' AND table_name = 'foo';");
  24.     if( $q ) $r = mysql_num_rows($q);
  25. }
  26. $end = microtime(1);
  27.  
  28. echo  ($end - $start) . " for select from schema num rows";
  29.  
  30. echo PHP_EOL;
  31. $start = microtime(1);
  32. for( $i = 0; $i < 100000; $i++ )
  33. {
  34.     $q = mysql_query("SELECT table_name FROM information_schema.tables WHERE table_schema = 'test' AND table_name = 'foo';");
  35.     if( $q ) $r = mysql_fetch_row($q);
  36. }
  37. $end = microtime(1);
  38.  
  39. echo  ($end - $start) . " for select from schema fetch row";
  40.  
  41. echo PHP_EOL;
  42.  
  43. $start = microtime(1);
  44. for( $i = 0; $i < 100000; $i++ )
  45. {
  46.     $q = mysql_query("SHOW TABLES FROM test");
  47.     while( $r = mysql_fetch_array($q) )
  48.     {
  49.         if($r['Tables_in_test'] == 'foo') break;
  50.     }
  51. }
  52. $end = microtime(1);
  53.  
  54. echo  ($end - $start) . " for SHOW TABLES FROM test";
  55.  
  56. echo PHP_EOL;
  57. echo ':::::::::::::::::::::::::BEGINNING EXISTING TABLE::::::::::::::::::::::::::::::';
  58. echo PHP_EOL;
  59. $start = microtime(1);
  60. for( $i = 0; $i < 100000; $i++ )
  61. {
  62.     mysql_query( 'SELECT 1 FROM bar' );
  63. }
  64. $end = microtime(1);
  65.  
  66. echo  ($end - $start) . " for good select";
  67.  
  68. echo PHP_EOL;
  69. $start = microtime(1);
  70. for( $i = 0; $i < 100000; $i++ )
  71. {
  72.     $q = mysql_query("SELECT table_name FROM information_schema.tables WHERE table_schema = 'test' AND table_name = 'bar';");
  73.     if( $q ) $r = mysql_num_rows($q);
  74. }
  75. $end = microtime(1);
  76.  
  77. echo  ($end - $start) . " for select from schema num rows";
  78. echo PHP_EOL;
  79.  
  80. $start = microtime(1);
  81. for( $i = 0; $i < 100000; $i++ )
  82. {
  83.     $q = mysql_query("SELECT table_name FROM information_schema.tables WHERE table_schema = 'test' AND table_name = 'bar';");
  84.     if( $q ) $r = mysql_fetch_row($q);
  85. }
  86. $end = microtime(1);
  87.  
  88. echo  ($end - $start) . " for select from schema fetch row";
  89. echo PHP_EOL;
  90.  
  91.  
  92. $start = microtime(1);
  93. for( $i = 0; $i < 100000; $i++ )
  94. {
  95.     $q = mysql_query("SHOW TABLES FROM test");
  96.     while( $r = mysql_fetch_array($q) )
  97.     {
  98.         if($r['Tables_in_test'] == 'bar') break;
  99.     }
  100. }
  101. $end = microtime(1);
  102.  
  103. echo  ($end - $start) . " for SHOW TABLES FROM test";
  104. echo PHP_EOL;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement