Ver Mensaje Individual
  #16 (permalink)  
Antiguo 04/05/2014, 11:45
Avatar de Italico76
Italico76
 
Fecha de Ingreso: abril-2007
Mensajes: 3.303
Antigüedad: 17 años, 1 mes
Puntos: 292
Respuesta: Reemplazar última coma por " y"

JO JO JO......Ud es un TRAMPOSO!!!! si Ud invierte el orden de las llamadas a las funciones la suya pierde lejos!!!!!!!!!!

Para 500:

Código resutlados pruebas:
Ver original
  1. Italico: 24.439208984375
  2. Alexis: 51.1572265625

y lo unico que hice fue cambiar el orden de las llamadas:

Código PHP:
Ver original
  1. <?php
  2. function italico(&$array)
  3. {
  4.     switch (count($array))
  5.     {
  6.     case 0: return null;
  7.     case 1: return $array[0];
  8.     default:    
  9.         $ult = array_pop($array);
  10.         return implode (' ,',$array).' y '.$ult;
  11.     }    
  12. }
  13.  
  14. function alexis($array) {
  15.     $string = implode(", ", $array);
  16.     return substr_replace ($string, " y ", strrpos($string, ","), 2);
  17. }
  18.  
  19. $array = range(1, 500);
  20.  
  21. $ini2 = microtime(true) * 1000;
  22. for ($i = 0; $i < 1000; $i++)
  23.     alexis($array);
  24. $fin2 = microtime(true) * 1000;
  25. $tiempoAlexis = $fin2 - $ini2;
  26.  
  27. $ini1 = microtime(true) * 1000;
  28. for ($i = 0; $i < 1000; $i++)
  29.     italico($array);
  30. $fin1 = microtime(true) * 1000;
  31. $tiempoItalico = $fin1 - $ini1;
  32.  
  33.  
  34.  
  35. echo "Italico: " . $tiempoItalico."\n";
  36. echo "Alexis: " . $tiempoAlexis;

El ultimo corre mas rapido ya que esos datos ya se procesaron y ni siquiera creando copias de los datos si son iguales el efecto "memoria" desaparece:

Código PHP:
Ver original
  1. <?php
  2. function italico1(&$arr)
  3.     {
  4.         $cant = count($arr);    
  5.    
  6.         if ($cant==0)  return null;        
  7.         $salida = null;
  8.        
  9.         if ($cant>1)
  10.         {
  11.             for ($i=0;$i<$cant-2;$i++)
  12.                 $salida .= $arr[$i].' ,';
  13.        
  14.             return $salida.$arr[$cant-2].' y '.$arr[$cant-1];  
  15.            
  16.         }else
  17.             return $arr[0];      
  18.    
  19.     }
  20.  
  21. function italico2(&$arr)
  22. {
  23.     switch (count($arr))
  24.     {
  25.     case 0: return null;
  26.     case 1: return $arr[0];
  27.     default:    
  28.         $ult = array_pop($arr);
  29.         return implode (' ,',$arr).' y '.$ult;
  30.     }    
  31. }
  32.  
  33. function alexis($array) {
  34.     $string = implode(", ", $array);
  35.     return substr_replace ($string, " y ", strrpos($string, ","), 2);
  36. }
  37.  
  38. $a[] = range(1, 500);
  39. $a[] = range(1, 500);
  40. $a[] = range(1, 500);
  41.  
  42.  
  43. $ini2 = microtime(true) * 1000;
  44. for ($i = 0; $i < 1000; $i++)
  45.     alexis($a[1]);
  46. $fin2 = microtime(true) * 1000;
  47. $tiempoAlexis = $fin2 - $ini2;
  48.  
  49. $ini1 = microtime(true) * 1000;
  50. for ($i = 0; $i < 1000; $i++)
  51.     italico1($a[0]);
  52. $fin1 = microtime(true) * 1000;
  53. $tiempoItalico1 = $fin1 - $ini1;
  54.  
  55. $ini1 = microtime(true) * 1000;
  56. for ($i = 0; $i < 1000; $i++)
  57.     italico2($a[2]);
  58. $fin1 = microtime(true) * 1000;
  59. $tiempoItalico2 = $fin1 - $ini1;
  60.  
  61.  
  62. echo "Italico (script #1): " . $tiempoItalico1."\n";
  63. echo "Italico (script #2): " . $tiempoItalico2."\n";
  64. echo "Alexis: " . $tiempoAlexis;

Puede probar...y si sigue cambiando de lugar las llamadas..... con los mismos datos, mismo "problema"
__________________
Salu2!

Última edición por Italico76; 04/05/2014 a las 11:55