[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: aplicacio per convertir numeros en "humans"



El Fri, Apr 25, 2008 at 04:14:38PM +0200, Jaume Sabater ens deleit�mb les seg�paraules:
[...]
> El Friday 25 April 2008 16:06:47 Pere Nubiola Radigales va escriure:
>> #include <stdlib.h>
>> #include <stdio.h>
>>
>> int main(int argc , char *argv[]) 
>> { 
>>       long num; 
>>       if (argc < 2 ) then return 0; 
>>       num = atol(argv[1]); 
>>       if ((num /1000000000) > 1) { 
>>           printf("%i G" , num /1000000000) ; 
>>           return 0; 
>>        } 
>>       if ((num /1000000) > 1) { 
>>           printf("%i M" , num /1000000) ; 
>>           return 0; 
>>        } 
>>       if ((num /1000) > 1) printf("%i K" , num /1000) ; 
>>       return 0;
}

Li falta aquesta ultima clau, que sino no ens compila ;) (be, i un 
"bashisme" que s'ha quedat "sueltu" per alla... :P)

>> 2008/4/25, Jaume Sabater <jaume@argus.net>:
>>> Hola
>>>
>>>  Algu sap d'alguna aplicacio en que quan li passis un numero te'l retorni 
>>> en format "huma"?
>>>
>>>  Es a dir, si se li passa el 10000 que et retorni un "10K", si se li 
>>> passa el 1000000 et retorni "1M".
>>>
>>>  Es per substituir una funcio que tinc feta en bash:
>>>
>>>  Human() 
>>>  { 
>>>         [ $1 -gt 10000000000 ] && echo -n ` expr $1 / 1000000000 `"G" 
>>>         [ $1 -gt 10000000 -a $1 -lt 10000000000 ] && echo -n ` expr $1 / 
>>>  1000000 `"M" 
>>>         [ $1 -gt 10000 -a $1 -lt 10000000 ] && echo -n ` expr $1 / 1000 
>>> `"K" }

La velocitat (o millor dit la seva falta) segurament vingui donada per dos 
motius:

- [/test: es un executable, i com a tal s'ha de fer un fork+exec al 
  executar-lo, amb totes les consequencies que te (crear un nou proces, 
  buscar l'executable al sistema de fitxers, llegir-lo, carregar-lo en 
  memoria i finalment fer que el nou proces l'executi, a part de recollir 
  el que aquest nou procis "escupi" per pantalla per a continuar-ho 
  processant en l'script).

- expr: li passa el mateix

Per a fer anar l'script mes rapid, o be utilitzes un programet en C, o be 
intentes no fer servir "tants" programes externs al bash. La solucio en 
bash et sera molt mes desitjable en el cas que cridis la funcio Human des 
del mateix script (o de d'un que hagi fet un 'source' de l'script que conte 
Human).

El cas, que aixo segurament funcionara mes rapid:

Human ()
{
  local num=$1
  if [ $num -ge 1000000000 ]; then
    echo -n ${num%[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]}"G"
  elif [ $num -ge 1000000 ]; then
    echo -n ${num%[0-9][0-9][0-9][0-9][0-9][0-9]}"M"
  elif [ $num -ge 1000 ]; then
    echo -n ${num%[0-9][0-9][0-9]}"K"
  else
    echo -n $num
  fi
}

Com que 'expr' trunca el resultat per sota, la substitucio de caracters et 
funciona igual de be, i com que es una "feature" embebida en bash (que no 
sh), et funcionara mes rapid. A mes a mes, aixi fas el minim nombre de 
comprobacions, enlloc de dempre 3.

apa!

-- 
 "And it's much the same thing with knowledge, for whenever you learn
 something new, the whole world becomes that much richer."
 -- The Princess of Pure Reason, as told by Norton Juster in The Phantom
 Tollbooth
 
 Listening: Node (As God Kills) 7. Watcher Of The Failed Generation


Reply to: