[comp.unix.questions] incrementing variables

glex@uh.msc.umn.edu (Jeffrey Gleixner) (03/29/90)

A couple of weeks ago someone asked how to increment a variable in Bsh and
in response to that it was mentioned that writting a C program to do it
would be much quicker.   I wondered how much faster it would actually be
and here's what I experienced.

%> cat t1
#!/bin/sh
count=0
while [ "$count" -lt "250" ]; do
        count=`expr $count + 1`
done
echo "count = $count"

%cat t2
#!/bin/sh
count=0
while [ "$count" -lt "250" ]; do
 count=`a1 "$count"`
done
echo "count = $count"

%>cat a1.c
#include <stdio.h>
main(argc,argv)
int argc; char *argv[]; {printf("%d",atoi(argv[1])+1);}

%> time t1
count = 250
7.5u 19.5s 0:31 85% 0+88k 1+3io 0pf+0w

%> time t2 
count = 250
7.3u 17.9s 0:29 85% 0+88k 1+3io 0pf+0w

I don't really see that much of an increase.  This was on a Sun 4/280.  If
it can be done more efficiently please E-Mail me 'cause I'm curious.  I also
tried using BC instead of the C program and that was horrible.  Any examples?

Thanks.
glex@msc.umn.edu  ===  " I don't date any girl that says bitch'n " ===

guy@auspex.auspex.com (Guy Harris) (03/30/90)

>A couple of weeks ago someone asked how to increment a variable in Bsh and
>in response to that it was mentioned that writting a C program to do it
>would be much quicker.

I presume they meant that doing the whole loop in C would be faster;
it's not particularly surprising that your program isn't faster, since a
lot of the time in both examples is spent firing up "exec" or your
program, and only a small amount of time is spent in the "expr" example
parsing the expression, etc..