0

Could someone please explain why it takes about 16 seconds from my Python script to complete while it takes about 194 seconds (!!) from the Shell script to complete on the same machine.

Here are my scripts:

 #!/usr/bin/env bash

for i in `seq 1 10000000`;
do
    echo -e $i '\t' 200000;
done

and

#!/usr/bin/python

for x in range(1, 10000001):
    print "%d \t 200000" % (x)
Rlearner
  • 351
  • 1
  • 4
  • 13
  • 1
    You would have to benchmark each part to be sure, but `seq 1 10000000` creates and runs a child process, so my guess (not an answer) would be that this would be a part of the difference. Please note that back-ticks are generally considered to be poor practice, with `$(...)` the preferred syntax. In bash or ksh93, you could use the `for (( i=0; i < 100000000; i++ ))` counting loop instead. In python 2 you might get slightly better performance using `xrange` instead of `range`. – cdarke Oct 05 '15 at 15:01
  • @jonrsharpe I have seen [this](https://stackoverflow.com/questions/2424921/python-vs-bash-in-which-kind-of-tasks-each-one-outruns-the-other-performance-w) question but its answers are way to general. My question is for a particular example. Do `seq` and `echo` in the Shell script call a number of other commands which make it slow? – Rlearner Oct 05 '15 at 15:05
  • Note that `seq` is a separate program, it has nothing to do with the shell. It is more than likely the storage of the large output sequence from the `seq` program which is the issue. – cdarke Oct 05 '15 at 15:08

0 Answers0