PageRenderTime 40ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/release/src/router/php/Zend/micro_bench.php

https://gitlab.com/envieidoc/tomato
PHP | 358 lines | 307 code | 50 blank | 1 comment | 37 complexity | 167da9cac8eb6aecebeaebf9f4616eb1 MD5 | raw file
  1. <?php
  2. function hallo() {
  3. }
  4. function simpleucall($n) {
  5. for ($i = 0; $i < $n; $i++)
  6. hallo();
  7. }
  8. function simpleudcall($n) {
  9. for ($i = 0; $i < $n; $i++)
  10. hallo2();
  11. }
  12. function hallo2() {
  13. }
  14. function simpleicall($n) {
  15. for ($i = 0; $i < $n; $i++)
  16. func_num_args();
  17. }
  18. class Foo {
  19. static $a = 0;
  20. public $b = 0;
  21. const TEST = 0;
  22. static function read_static($n) {
  23. for ($i = 0; $i < $n; ++$i) {
  24. $x = self::$a;
  25. }
  26. }
  27. static function write_static($n) {
  28. for ($i = 0; $i < $n; ++$i) {
  29. self::$a = 0;
  30. }
  31. }
  32. static function isset_static($n) {
  33. for ($i = 0; $i < $n; ++$i) {
  34. $x = isset(self::$a);
  35. }
  36. }
  37. static function empty_static($n) {
  38. for ($i = 0; $i < $n; ++$i) {
  39. $x = empty(self::$a);
  40. }
  41. }
  42. static function f() {
  43. }
  44. static function call_static($n) {
  45. for ($i = 0; $i < $n; ++$i) {
  46. self::f();
  47. }
  48. }
  49. function read_prop($n) {
  50. for ($i = 0; $i < $n; ++$i) {
  51. $x = $this->b;
  52. }
  53. }
  54. function write_prop($n) {
  55. for ($i = 0; $i < $n; ++$i) {
  56. $this->b = 0;
  57. }
  58. }
  59. function assign_add_prop($n) {
  60. for ($i = 0; $i < $n; ++$i) {
  61. $this->b += 2;
  62. }
  63. }
  64. function pre_inc_prop($n) {
  65. for ($i = 0; $i < $n; ++$i) {
  66. ++$this->b;
  67. }
  68. }
  69. function pre_dec_prop($n) {
  70. for ($i = 0; $i < $n; ++$i) {
  71. --$this->b;
  72. }
  73. }
  74. function post_inc_prop($n) {
  75. for ($i = 0; $i < $n; ++$i) {
  76. $this->b++;
  77. }
  78. }
  79. function post_dec_prop($n) {
  80. for ($i = 0; $i < $n; ++$i) {
  81. $this->b--;
  82. }
  83. }
  84. function isset_prop($n) {
  85. for ($i = 0; $i < $n; ++$i) {
  86. $x = isset($this->b);
  87. }
  88. }
  89. function empty_prop($n) {
  90. for ($i = 0; $i < $n; ++$i) {
  91. $x = empty($this->b);
  92. }
  93. }
  94. function g() {
  95. }
  96. function call($n) {
  97. for ($i = 0; $i < $n; ++$i) {
  98. $this->g();
  99. }
  100. }
  101. function read_const($n) {
  102. for ($i = 0; $i < $n; ++$i) {
  103. $x = $this::TEST;
  104. }
  105. }
  106. }
  107. function read_static($n) {
  108. for ($i = 0; $i < $n; ++$i) {
  109. $x = Foo::$a;
  110. }
  111. }
  112. function write_static($n) {
  113. for ($i = 0; $i < $n; ++$i) {
  114. Foo::$a = 0;
  115. }
  116. }
  117. function isset_static($n) {
  118. for ($i = 0; $i < $n; ++$i) {
  119. $x = isset(Foo::$a);
  120. }
  121. }
  122. function empty_static($n) {
  123. for ($i = 0; $i < $n; ++$i) {
  124. $x = empty(Foo::$a);
  125. }
  126. }
  127. function call_static($n) {
  128. for ($i = 0; $i < $n; ++$i) {
  129. Foo::f();
  130. }
  131. }
  132. function create_object($n) {
  133. for ($i = 0; $i < $n; ++$i) {
  134. $x = new Foo();
  135. }
  136. }
  137. define('TEST', null);
  138. function read_const($n) {
  139. for ($i = 0; $i < $n; ++$i) {
  140. $x = TEST;
  141. }
  142. }
  143. function read_auto_global($n) {
  144. for ($i = 0; $i < $n; ++$i) {
  145. $x = $_GET;
  146. }
  147. }
  148. $g_var = 0;
  149. function read_global_var($n) {
  150. for ($i = 0; $i < $n; ++$i) {
  151. $x = $GLOBALS['g_var'];
  152. }
  153. }
  154. function read_hash($n) {
  155. $hash = array('test' => 0);
  156. for ($i = 0; $i < $n; ++$i) {
  157. $x = $hash['test'];
  158. }
  159. }
  160. function read_str_offset($n) {
  161. $str = "test";
  162. for ($i = 0; $i < $n; ++$i) {
  163. $x = $str[1];
  164. }
  165. }
  166. function issetor($n) {
  167. $val = array(0,1,2,3,4,5,6,7,8,9);
  168. for ($i = 0; $i < $n; ++$i) {
  169. $x = $val ?: null;
  170. }
  171. }
  172. function issetor2($n) {
  173. $f = false; $j = 0;
  174. for ($i = 0; $i < $n; ++$i) {
  175. $x = $f ?: $j + 1;
  176. }
  177. }
  178. function ternary($n) {
  179. $val = array(0,1,2,3,4,5,6,7,8,9);
  180. $f = false;
  181. for ($i = 0; $i < $n; ++$i) {
  182. $x = $f ? null : $val;
  183. }
  184. }
  185. function ternary2($n) {
  186. $f = false; $j = 0;
  187. for ($i = 0; $i < $n; ++$i) {
  188. $x = $f ? $f : $j + 1;
  189. }
  190. }
  191. /*****/
  192. function empty_loop($n) {
  193. for ($i = 0; $i < $n; ++$i) {
  194. }
  195. }
  196. function getmicrotime()
  197. {
  198. $t = gettimeofday();
  199. return ($t['sec'] + $t['usec'] / 1000000);
  200. }
  201. function start_test()
  202. {
  203. ob_start();
  204. return getmicrotime();
  205. }
  206. function end_test($start, $name, $overhead = null)
  207. {
  208. global $total;
  209. global $last_time;
  210. $end = getmicrotime();
  211. ob_end_clean();
  212. $last_time = $end-$start;
  213. $total += $last_time;
  214. $num = number_format($last_time,3);
  215. $pad = str_repeat(" ", 24-strlen($name)-strlen($num));
  216. if (is_null($overhead)) {
  217. echo $name.$pad.$num."\n";
  218. } else {
  219. $num2 = number_format($last_time - $overhead,3);
  220. echo $name.$pad.$num." ".$num2."\n";
  221. }
  222. ob_start();
  223. return getmicrotime();
  224. }
  225. function total()
  226. {
  227. global $total;
  228. $pad = str_repeat("-", 24);
  229. echo $pad."\n";
  230. $num = number_format($total,3);
  231. $pad = str_repeat(" ", 24-strlen("Total")-strlen($num));
  232. echo "Total".$pad.$num."\n";
  233. }
  234. const N = 5000000;
  235. $t0 = $t = start_test();
  236. empty_loop(N);
  237. $t = end_test($t, 'empty_loop');
  238. $overhead = $last_time;
  239. simpleucall(N);
  240. $t = end_test($t, 'func()', $overhead);
  241. simpleudcall(N);
  242. $t = end_test($t, 'undef_func()', $overhead);
  243. simpleicall(N);
  244. $t = end_test($t, 'int_func()', $overhead);
  245. Foo::read_static(N);
  246. $t = end_test($t, '$x = self::$x', $overhead);
  247. Foo::write_static(N);
  248. $t = end_test($t, 'self::$x = 0', $overhead);
  249. Foo::isset_static(N);
  250. $t = end_test($t, 'isset(self::$x)', $overhead);
  251. Foo::empty_static(N);
  252. $t = end_test($t, 'empty(self::$x)', $overhead);
  253. read_static(N);
  254. $t = end_test($t, '$x = Foo::$x', $overhead);
  255. write_static(N);
  256. $t = end_test($t, 'Foo::$x = 0', $overhead);
  257. isset_static(N);
  258. $t = end_test($t, 'isset(Foo::$x)', $overhead);
  259. empty_static(N);
  260. $t = end_test($t, 'empty(Foo::$x)', $overhead);
  261. Foo::call_static(N);
  262. $t = end_test($t, 'self::f()', $overhead);
  263. call_static(N);
  264. $t = end_test($t, 'Foo::f()', $overhead);
  265. $x = new Foo();
  266. $x->read_prop(N);
  267. $t = end_test($t, '$x = $this->x', $overhead);
  268. $x->write_prop(N);
  269. $t = end_test($t, '$this->x = 0', $overhead);
  270. $x->assign_add_prop(N);
  271. $t = end_test($t, '$this->x += 2', $overhead);
  272. $x->pre_inc_prop(N);
  273. $t = end_test($t, '++$this->x', $overhead);
  274. $x->pre_dec_prop(N);
  275. $t = end_test($t, '--$this->x', $overhead);
  276. $x->post_inc_prop(N);
  277. $t = end_test($t, '$this->x++', $overhead);
  278. $x->post_dec_prop(N);
  279. $t = end_test($t, '$this->x--', $overhead);
  280. $x->isset_prop(N);
  281. $t = end_test($t, 'isset($this->x)', $overhead);
  282. $x->empty_prop(N);
  283. $t = end_test($t, 'empty($this->x)', $overhead);
  284. $x->call(N);
  285. $t = end_test($t, '$this->f()', $overhead);
  286. $x->read_const(N);
  287. $t = end_test($t, '$x = Foo::TEST', $overhead);
  288. create_object(N);
  289. $t = end_test($t, 'new Foo()', $overhead);
  290. read_const(N);
  291. $t = end_test($t, '$x = TEST', $overhead);
  292. read_auto_global(N);
  293. $t = end_test($t, '$x = $_GET', $overhead);
  294. read_global_var(N);
  295. $t = end_test($t, '$x = $GLOBALS[\'v\']', $overhead);
  296. read_hash(N);
  297. $t = end_test($t, '$x = $hash[\'v\']', $overhead);
  298. read_str_offset(N);
  299. $t = end_test($t, '$x = $str[0]', $overhead);
  300. issetor(N);
  301. $t = end_test($t, '$x = $a ?: null', $overhead);
  302. issetor2(N);
  303. $t = end_test($t, '$x = $f ?: tmp', $overhead);
  304. ternary(N);
  305. $t = end_test($t, '$x = $f ? $f : $a', $overhead);
  306. ternary2(N);
  307. $t = end_test($t, '$x = $f ? $f : tmp', $overhead);
  308. total($t0, "Total");