简体   繁体   中英

How to write a script to run a program multiple times and store the time in a file?

I am trying to run a script to run my program written in C that is just a simple sorting program to compare it to the built in function in unix. I want to take the user time from both sort (unix) and my sort function 100 times and put it in a file excel, csv to compare the algorithms.

Both my function and the sort function work when I run it manually but I dont know how to write code to automate this process for 100 times.

#!/bin/sh
for i in {1..100}
do
     for t in 01
     do
     echo ===Test $t ====
     time sort -n <tests/$t > tests/$t.expected
     time ./useIntList <tests/$t> tests/$t.observed
     done
done
rm tests/*.expected tests/*.obsereved

I get the program run 100 times but I dont know how to get the user time into an array and print it into a file.

time writes its output to stderr , not stdout . It's also a shell built-in that executes the command after it, including the I/O redirections, outside the timing. So you need to put the time command in a block, and redirect outside the block.

You should also use #!/bin/bash . Syntax like {1..100} is a bash extension, it might not be available when you use #!/bin/sh .

#!/bin/sh
for i in {1..100}
do
     for t in 01
     do
     echo ===Test $t ====
     { time sort -n <tests/$t > tests/$t.expected; } 2>tests/$t.time.expected
     { time ./useIntList <tests/$t> tests/$t.observed; } 2>tests/$t.time.observed
     done
done
rm tests/*.expected tests/*.obsereved

The *.time.* files will contain the output of time .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM