Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> Alternatywna konstrukcja warunkowa., Co się szybciej wykona.
starach
post
Post #1





Grupa: Zarejestrowani
Postów: 999
Pomógł: 30
Dołączył: 14.01.2007
Skąd: wiesz ?

Ostrzeżenie: (0%)
-----


Witam.
Spieram się z kolegą o to który zapis metody tmpl_restore() wykona się szybciej.
Co wy o tym sądzicie. Mnie się wydaje że ten mniej czytelniejszy ( nie za bardzo wiem dlaczego, ale tak mi się wydaje)
  1. <?php
  2. public function tmpl_restore($index='')
  3. {
  4. $this->tmpl = $index=='' ? $this->tmpl_backup[count($this->tmpl_backup)] : isset($this->tmpl_backup[$index]) ? $this->tmpl_backup[$index] : die('Zly parametr. Template::tmpl_restore()');
  5. }
  6. public function tmpl_restore($index='')
  7. {
  8. if($index=='')
  9. {
  10. $this->tmpl = $this->tmpl_backup[count($this->tmpl_backup)];
  11. } else {
  12. if(isset($this->tmpl_backup[$index]))
  13. {
  14. $this->tmpl = $this->tmpl_backup[$index];
  15. } else {
  16. die('Zly parametr. Template::tmpl_restore()');
  17. }
  18. }
  19. }
  20. ?>

edit>
( nie za bardzo wiem dlaczego, ale tak mi się wydaje)
Nie potrafię tego właściwie uzasadnić.

Ten post edytował orglee 23.04.2007, 18:28:20
Go to the top of the page
+Quote Post
 
Start new topic
Odpowiedzi
starach
post
Post #2





Grupa: Zarejestrowani
Postów: 999
Pomógł: 30
Dołączył: 14.01.2007
Skąd: wiesz ?

Ostrzeżenie: (0%)
-----


Paranoja już sam nie wiem co jest jak.
@dr_bonzo: Prosiłbym cię uprzejmie żebyś spróbował przetestować to u siebie. Jakby jeszcze ktoś mógł to zrobić to byłbym wdzięczny.
  1. <?php
  2. function getmicrotime()
  3. {
  4. list($usec, $sec) = explode("",microtime());
  5. return ((float)$usec + (float)$sec);
  6. }
  7. function test_a($obj)
  8. {
  9. $start = getmicrotime();
  10. for($i=0; $i<1000; $i++)
  11. {
  12. $obj->tmpl_restore_a();
  13. }
  14. $end = getmicrotime();
  15. $time = $end - $start;
  16. return $time;
  17. }
  18. function test_b($obj)
  19. {
  20. $start = getmicrotime();
  21. for($i=0; $i<1000; $i++)
  22. {
  23. $obj->tmpl_restore_b();
  24. }
  25. $end = getmicrotime();
  26. $time = $end - $start;
  27. return $time;
  28. }
  29. function srednia($arr)
  30. {
  31. return array_sum($arr)/count($arr);
  32. }
  33. class Test
  34. {
  35. public $tmpl;
  36. public $tmpl_backup = array('Wpis pierwszy');
  37. public function __construct(){}
  38. public function tmpl_restore_a($index='')
  39. {
  40. $this->tmpl = ($index=='' ? $this->tmpl_backup[count($this->tmpl_backup)-1] : (isset($this->tmpl_backup[$index]) ? $this->tmpl_backup[$index] : die('Zly parametr. Template::tmpl_restore()')));
  41. }
  42. public function tmpl_restore_b($index='')
  43. {
  44. if($index=='')
  45. {
  46. $this->tmpl = $this->tmpl_backup[count($this->tmpl_backup)-1];
  47. } else {
  48. if(isset($this->tmpl_backup[$index]))
  49. {
  50. $this->tmpl = $this->tmpl_backup[$index];
  51. } else {
  52. die('Zly parametr. Template::tmpl_restore()');
  53. }
  54. }
  55. }
  56. }
  57. $test = new Test;
  58. for($i=0; $i<50; $i++)
  59. {
  60. $array_a[] = test_a($test);
  61. $array_b[] = test_b($test);
  62. }
  63. $sr_a = srednia($array_a);
  64. $sr_b = srednia($array_b);
  65. ?>
  66. <table border="1">
  67. <tr>
  68. <td align="center"><b>A(<?php echo $sr_a; ?>)</b><br>B(<?php echo $sr_b; ?>)</td>
  69. <td align="center"><b>B(<?php echo $sr_b; ?>)</b><br>A(<?php echo $sr_a; ?>)</td>
  70. </tr>
  71. <tr>
  72. <td valign="top"><?php echo '<pre>'.print_r($array_a, true).'</pre>'; ?></td>
  73. <td valign="top"><?php echo '<pre>'.print_r($array_b, true).'</pre>'; ?></td>
  74. </tr>
  75. </table>


Ten post edytował orglee 24.04.2007, 13:09:02
Go to the top of the page
+Quote Post

Posty w temacie


Reply to this topicStart new topic
2 Użytkowników czyta ten temat (2 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Aktualny czas: 10.10.2025 - 20:49