[I had written]

% awk '{ print | "dc" }

% NR > 1 { print "+" | "dc" }

% END { print "p" | "dc"

% close("dc")

% }' thefilename

But now I've sobered up and decided that this is simpler and probably

measurably faster if you have enough data:

awk '{ print $0 "+" | "dc" }

BEGIN { print "0" | "dc" }

END { print "p" | "dc"

close("dc")

}' thefilename

What this does, for anyone not familiar with dc, is feed dc a script

like this:

0

123456789876543212345+

23456789876543245667+

p

dc is a stack-based calculator. This input pushes 0 on the stack, then the

first number, then it adds them together and puts the result on the

stack, and so on unti the end, then it prints the number on the stack.

My first version does one fewer addition, at the cost of repeatedly

testing whether we're on the first line.

I was just curious about performance, so I ran some tests on all four

solutions I've put forward (adding up 70,002 numbers ranging from

6968382597342 to 768602705255855100000). These are the averages of

real, user, and system time reported by time for three runs of each

solution:

rexx (regina): 6.50s 6.34s 0.07s

nawk/bc: 17.73s 16.56s 0.95s

mawk/bc: 12.62s 12.28s 0.13s

nawk/dc(1): 42.30s 40.35s 1.41s

mawk/dc(1): 35.90s 35.50s 0.11s

nawk/dc(2): 41.81s 40.01s 1.36s

mawk/dc(2): 35.60s 35.34s 0.08s

I have GNU bc here because it allows variable names > 1 character, but

since the system bc is a preprocessor to dc, I have to assume it's a

lot faster than the system bc would be, too.

--

Patrick TJ McPhee

East York Canada