Re: aplicacio per convertir numeros en "humans"

2008-04-25 Conversa Lluis
El Fri, Apr 25, 2008 at 04:14:38PM +0200, Jaume Sabater ens deleit� amb les 
seg�ents paraules:
[...]
> El Friday 25 April 2008 16:06:47 Pere Nubiola Radigales va escriure:
>> #include 
>> #include 
>>
>> int main(int argc , char *argv[]) 
>> { 
>>   long num; 
>>   if (argc < 2 ) then return 0; 
>>   num = atol(argv[1]); 
>>   if ((num /10) > 1) { 
>>   printf("%i G" , num /10) ; 
>>   return 0; 
>>} 
>>   if ((num /100) > 1) { 
>>   printf("%i M" , num /100) ; 
>>   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 <[EMAIL PROTECTED]>:
>>> 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 1 que et retorni un "10K", si se li 
>>> passa el 100 et retorni "1M".
>>>
>>>  Es per substituir una funcio que tinc feta en bash:
>>>
>>>  Human() 
>>>  { 
>>> [ $1 -gt 100 ] && echo -n ` expr $1 / 10 `"G" 
>>> [ $1 -gt 1000 -a $1 -lt 100 ] && echo -n ` expr $1 / 
>>>  100 `"M" 
>>> [ $1 -gt 1 -a $1 -lt 1000 ] && 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 10 ]; 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 100 ]; 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


-- 
To UNSUBSCRIBE, email to [EMAIL PROTECTED]
with a subject of "unsubscribe". Trouble? Contact [EMAIL PROTECTED]



Re: aplicacio per convertir numeros en "humans"

2008-04-25 Conversa Jaume Sabater
Jajajajaja, acabes d'escriure lo que em volia evitar d'escriure :-)

Gracies, ets un sol :)


El Friday 25 April 2008 16:06:47 Pere Nubiola Radigales va escriure:
> #include 
> #include 
>
> int main(int argc , char *argv[])
> {
>   long num;
>   if (argc < 2 ) then return 0;
>   num = atol(argv[1]);
>   if ((num /10) > 1) {
>   printf("%i G" , num /10) ;
>   return 0;
>}
>   if ((num /100) > 1) {
>   printf("%i M" , num /100) ;
>   return 0;
>}
>   if ((num /1000) > 1) printf("%i K" , num /1000) ;
>   return 0;
>
> guardas a sobre human.c
> gcc -o human human.c
>
> 2008/4/25, Jaume Sabater <[EMAIL PROTECTED]>:
> > 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 1 que et retorni un "10K", si se li
> > passa el 100 et retorni "1M".
> >
> >  Es per substituir una funcio que tinc feta en bash:
> >
> >  Human()
> >  {
> > [ $1 -gt 100 ] && echo -n ` expr $1 / 10 `"G"
> > [ $1 -gt 1000 -a $1 -lt 100 ] && echo -n ` expr $1 /
> >  100 `"M"
> > [ $1 -gt 1 -a $1 -lt 1000 ] && echo -n ` expr $1 / 1000
> > `"K" }
> >
> >  Resulta que tinc un script on s'aplica intensivament, i m'interessaria
> >  agilitzar-ho.
> >
> >  En aquest script assumeixo que 1K=1000, pero si es 1K=1024 tambe em
> > serveix.
> >
> >  PD: Perdoneu els accents, des que vaig migrar el meu PC de Debian a
> > Ubuntu que tinc certs efectes secundaris que no he lograt resoldre...
> >  --
> >  Jaume Sabater
> >
> >
> >
> >  --
> >  To UNSUBSCRIBE, email to [EMAIL PROTECTED]
> >  with a subject of "unsubscribe". Trouble? Contact
> > [EMAIL PROTECTED]
>
> --
> Pere Nubiola Radigales
> Telf: +34 656316974
> e-mail: [EMAIL PROTECTED]
>[EMAIL PROTECTED]



-- 
Jaume Sabater


-- 
To UNSUBSCRIBE, email to [EMAIL PROTECTED]
with a subject of "unsubscribe". Trouble? Contact [EMAIL PROTECTED]



Re: aplicacio per convertir numeros en "humans"

2008-04-25 Conversa Jaume Sabater
Algo molt similar pero sense les funcionalitats del du.

Es a dir, els numerets ja els tinc previament, no m'interessa guardar-los en 
fomrat "huma", sino en unitats. Em serveixen per fer calculs, i vull donar 
als resultats el format huma, un cop els calculs estan fets.

El Friday 25 April 2008 15:48:04 xab ara ele va escriure:
> ~~>A Divendres 25 Abril 2008 15:11, Jaume Sabater sembla que va escriure:
> ~ 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 1 que et retorni un "10K", si se li passa
>  el ~ 100 et retorni "1M".
> ~
> ~ Es per substituir una funcio que tinc feta en bash:
> ~
> ~ Human()
> ~ {
> ~ [ $1 -gt 100 ] && echo -n ` expr $1 / 10 `"G"
> ~ [ $1 -gt 1000 -a $1 -lt 100 ] && echo -n ` expr $1 /
> ~ 100 `"M"
> ~ [ $1 -gt 1 -a $1 -lt 1000 ] && echo -n ` expr $1 / 1000
>  `"K" ~ }
> ~
> ~ Resulta que tinc un script on s'aplica intensivament, i m'interessaria
> ~ agilitzar-ho.
> ~
> ~ En aquest script assumeixo que 1K=1000, pero si es 1K=1024 tambe em
>  serveix. ~
> ~ PD: Perdoneu els accents, des que vaig migrar el meu PC de Debian a
> Ubuntu que ~ tinc certs efectes secundaris que no he lograt resoldre...
> ~ --
> ~ Jaume Sabater
> ~
> ~
>
>
> hola,
>
> no sé si servirà de molt, perquè en sé poquet; quan fas #  du -h
> el paràmetre -h ho fa human potser pots reconduir-ne el funcionament...
>
> i disculpa si pixo molt fora de test  ;-)
>
> salut
>
> --
> xab



-- 
Jaume Sabater



Re: aplicacio per convertir numeros en "humans"

2008-04-25 Conversa Ernest Adrogué
25/04/08 @ 15:11 (+0200), thus spake Jaume Sabater:
> 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 1 que et retorni un "10K", si se li passa el 
> 100 et retorni "1M".
> 
> Es per substituir una funcio que tinc feta en bash:

Pots provar amb l'units:

[EMAIL PROTECTED]:~$ units -t '1 bytes' 'kilobytes'
10
[EMAIL PROTECTED]:~$ 

> Resulta que tinc un script on s'aplica intensivament, i m'interessaria 
> agilitzar-ho.

El que no tinc clar és que això sigui més àgil que l'script
que has fet...

Ernest



Re: aplicacio per convertir numeros en "humans"

2008-04-25 Conversa Pere Nubiola Radigales
#include 
#include 

int main(int argc , char *argv[])
{
  long num;
  if (argc < 2 ) then return 0;
  num = atol(argv[1]);
  if ((num /10) > 1) {
  printf("%i G" , num /10) ;
  return 0;
   }
  if ((num /100) > 1) {
  printf("%i M" , num /100) ;
  return 0;
   }
  if ((num /1000) > 1) printf("%i K" , num /1000) ;
  return 0;

guardas a sobre human.c
gcc -o human human.c



2008/4/25, Jaume Sabater <[EMAIL PROTECTED]>:
> 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 1 que et retorni un "10K", si se li passa el
>  100 et retorni "1M".
>
>  Es per substituir una funcio que tinc feta en bash:
>
>  Human()
>  {
> [ $1 -gt 100 ] && echo -n ` expr $1 / 10 `"G"
> [ $1 -gt 1000 -a $1 -lt 100 ] && echo -n ` expr $1 /
>  100 `"M"
> [ $1 -gt 1 -a $1 -lt 1000 ] && echo -n ` expr $1 / 1000 `"K"
>  }
>
>  Resulta que tinc un script on s'aplica intensivament, i m'interessaria
>  agilitzar-ho.
>
>  En aquest script assumeixo que 1K=1000, pero si es 1K=1024 tambe em serveix.
>
>  PD: Perdoneu els accents, des que vaig migrar el meu PC de Debian a Ubuntu 
> que
>  tinc certs efectes secundaris que no he lograt resoldre...
>  --
>  Jaume Sabater
>
>
>
>  --
>  To UNSUBSCRIBE, email to [EMAIL PROTECTED]
>  with a subject of "unsubscribe". Trouble? Contact [EMAIL PROTECTED]
>
>


-- 
Pere Nubiola Radigales
Telf: +34 656316974
e-mail: [EMAIL PROTECTED]
   [EMAIL PROTECTED]


-- 
To UNSUBSCRIBE, email to [EMAIL PROTECTED]
with a subject of "unsubscribe". Trouble? Contact [EMAIL PROTECTED]



Re: aplicacio per convertir numeros en "humans"

2008-04-25 Conversa xab ara ele
~~>A Divendres 25 Abril 2008 15:11, Jaume Sabater sembla que va escriure:
~ 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 1 que et retorni un "10K", si se li passa
 el ~ 100 et retorni "1M".
~
~ Es per substituir una funcio que tinc feta en bash:
~
~ Human()
~ {
~ [ $1 -gt 100 ] && echo -n ` expr $1 / 10 `"G"
~ [ $1 -gt 1000 -a $1 -lt 100 ] && echo -n ` expr $1 /
~ 100 `"M"
~ [ $1 -gt 1 -a $1 -lt 1000 ] && echo -n ` expr $1 / 1000
 `"K" ~ }
~
~ Resulta que tinc un script on s'aplica intensivament, i m'interessaria
~ agilitzar-ho.
~
~ En aquest script assumeixo que 1K=1000, pero si es 1K=1024 tambe em
 serveix. ~
~ PD: Perdoneu els accents, des que vaig migrar el meu PC de Debian a Ubuntu
 que ~ tinc certs efectes secundaris que no he lograt resoldre...
~ --
~ Jaume Sabater
~
~


hola,

no sé si servirà de molt, perquè en sé poquet; quan fas #  du -h
el paràmetre -h ho fa human potser pots reconduir-ne el funcionament...

i disculpa si pixo molt fora de test  ;-)

salut

--
xab



aplicacio per convertir numeros en "humans"

2008-04-25 Conversa Jaume Sabater
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 1 que et retorni un "10K", si se li passa el 
100 et retorni "1M".

Es per substituir una funcio que tinc feta en bash:

Human()
{
[ $1 -gt 100 ] && echo -n ` expr $1 / 10 `"G"
[ $1 -gt 1000 -a $1 -lt 100 ] && echo -n ` expr $1 / 
100 `"M"
[ $1 -gt 1 -a $1 -lt 1000 ] && echo -n ` expr $1 / 1000 `"K"
}

Resulta que tinc un script on s'aplica intensivament, i m'interessaria 
agilitzar-ho.

En aquest script assumeixo que 1K=1000, pero si es 1K=1024 tambe em serveix.

PD: Perdoneu els accents, des que vaig migrar el meu PC de Debian a Ubuntu que 
tinc certs efectes secundaris que no he lograt resoldre...
-- 
Jaume Sabater


-- 
To UNSUBSCRIBE, email to [EMAIL PROTECTED]
with a subject of "unsubscribe". Trouble? Contact [EMAIL PROTECTED]