Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> [php] each() funkcja przestarzała w php 7.2?
kukix
post
Post #1





Grupa: Zarejestrowani
Postów: 600
Pomógł: 2
Dołączył: 1.09.2002
Skąd: Wrocław

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


Witam.

W ostatnim czasie zauważyłem ,ze na liscie funkcji deprecjonowanych jest each()

Od jakiegoś czasu przerabiałem pętle foreach() na pętle " while (list($id, $nazwa) = each($my_array)) { .. } " .

Widziałem różne testy, gdzie pętla while była szybsza, podobno nie robi w pamięcie serwera kopii tablicy jak pętla foreach().


Jaka jest Wasza opinia na ten temat? Muszę spowrotem zamieniać na foreach() ?



Go to the top of the page
+Quote Post
 
Start new topic
Odpowiedzi
Pyton_000
post
Post #2





Grupa: Zarejestrowani
Postów: 8 068
Pomógł: 1414
Dołączył: 26.10.2005

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


Masz... żeby nie było że się czepiam:


  1. <?php
  2. print "While vs Foreach benchmark" . PHP_EOL;
  3.  
  4. $elements = [
  5. 1,
  6. 10,
  7. 100,
  8. 1000,
  9. 10000,
  10. 100000,
  11. 1000000,
  12. 10000000,
  13. 100000000,
  14. ];
  15.  
  16. foreach ($elements as $element) {
  17. print "Starting test with $element elements" . PHP_EOL;
  18. print "Start Generating elements" . PHP_EOL;
  19. $start = microtime(true);
  20. $data = range(1, $element);
  21. print "Finished generating elements: " . (microtime(true) - $start) . PHP_EOL;
  22.  
  23. reset($data);
  24. print "Start while()" . PHP_EOL;
  25. $start = microtime(true);
  26. while (list($a, $b) = each($data)) {
  27.  
  28. }
  29. print "Finished while: " . (microtime(true) - $start) . PHP_EOL;
  30.  
  31. print "Start foreach()" . PHP_EOL;
  32. reset($data);
  33. $start = microtime(true);
  34. foreach ($data as $a => $b) {
  35.  
  36. }
  37. print "Finished foreach: " . (microtime(true) - $start) . PHP_EOL;
  38. print PHP_EOL.PHP_EOL;
  39. }


i wyniki:
http://bin.devsphp.pl/fe4c17dbf540f3403a3e...8bfe037b676.sql
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: 8.10.2025 - 04:08