Register
It is currently Tue Jul 29, 2014 2:54 pm

speed_test: 'cat' vs 'dog'


All times are UTC - 6 hours


Post new topic Reply to topic  [ 4 posts ] 
Author Message
 PostPosted: Mon Feb 01, 2010 1:42 am   
User avatar

Joined: Mon Jan 18, 2010 8:10 pm
Posts: 40
I wrote a simple (meta)program to run two analogous programs, 'cat' and 'dog', back-to-back and calculate the total time for each over several (five) passes of a file.

It is really quite a simple script, but the speed-testing idea seems interesting, and there are a couple of points in it that were worth learning.

Code:
#=======================================================================#
#   NAME:       speed_test: 'cat' vs 'dog'         
#   DESCRIPTION:    runs two programs, 'cat and 'dog', one after the
#         other, timing the duration of run-time over five
#         runs, and outputs total times (and possibly,   
#         eventually, averages).            
#   ARTHUR:   G O Free =:)                  
#   FOR: bashscripts.org                  
#   DATE: February 1, 2010                  
#=======================================================================#
#
# This method uses the 'reserved word' in the bash shell, 'time', as
# opposed to the external GNU command of the same name. It is like a
# built-in and is formatted using the environment variable 'TIMEFORMAT'.
# --> Default format: TIMEFORMAT=$'\nreal\t%3lR\nuser\t%3lU\nsys\t%3lS'
# --> Format for Real Time in Seconds (No labels): TIMEFORMAT='%3R'
#
# The line, '{ time [command] > /dev/null 2>&1 ; } 2>> time.txt',
# suppresses the output of the command (eg. 'cat' or 'dog') to standard
# output and redirects the output of 'time' to a file record.
#
# Alternatively use the line,
# '{ time cat $TEST_FILE ; } 2>> cattime.txt', if you wish to view the
# outputs of 'cat' and 'dog'. If you do, try it with 'more' or 'less'.
#
# This script also uses the GNU "arbitrary precision numeric processing
# language", 'bc', to do the math. Variables in 'bash' script are piped to 'bc'
# which makes the required calculations.
#
               # Display name of program
echo; echo "speed_test: 'cat' vs 'dog'"
               # Set the format for 'time'
               # output (real time only, in
               # seconds, no letters/labels)
TIMEFORMAT='%3R'
               # Test that a filename was
               # given and the file exists
TEST_FILE=${1:?"No file given: Usage: speed_test [filename]"}
if [ ! -e $TEST_FILE ]; then echo "FILE DOES NOT EXIST"; exit 0; fi
               # Display name of test file
echo "TEST_FILE: $TEST_FILE"; echo

### Run test with 'cat' first
               # Remove previous records and
               # start new record for first
               # program, 'cat'
if [ -e cattime.txt ]; then rm cattime.txt; fi
touch cattime.txt
               # Signal start of 'cat' test
echo "CAT"
echo "time cat $TEST_FILE:"
               # Run 'cat' on test file five
               # times
for (( i = 1; i <= 5; i++)); do
               # Identify run number
   echo -n -e "$i)\t "
               # Run 'cat' on test file,
               # Suppress 'cat' display,
               # and record run-times
   { time cat $TEST_FILE > /dev/null 2>&1 ; } 2>> cattime.txt
               # Use this line to display
               # 'cat' output
#   { time cat $TEST_FILE ; } 2>> cattime.txt
               # Display run-time
   cat cattime.txt | tail -n 1
done
echo
               # Start count of recorded
               # run-times
let -i COUNT=1
               # Start run-time total at zero,
               # sending value to 'bc' for
               # decimal numbers
CAT_TOTAL=$(echo "0" | bc)
               # Signal start of summary
echo "total time for 'cat':"
               # Read each run-time in
               # recorded list
for TIME in $(cat cattime.txt); do
               # Convert recorded times to
               # decimal numbers with 'bc'
   DECT=$(echo "$TIME" | bc)
               # Display times and decimal
               # values
   echo -e "$COUNT) TIME: $TIME\t DECT: $DECT"
               # Use 'bc' to calculate the sum
               # of 'cat' run-times
   CAT_TOTAL=$(echo "$CAT_TOTAL+$DECT" | bc)
   let COUNT=$COUNT+1
done
               # Display run-time total,
               # '$CAT_TOTAL'
echo -e "\t\tTOTAL: $CAT_TOTAL"
echo

### Run the test a second time with 'dog'
               # Remove previous records and
               # start new record for second
               # program, 'dog'
if [ -e dogtime.txt ]; then rm dogtime.txt; fi
touch dogtime.txt
               # Signal start of 'dog' test
echo "DOG"
echo "time dog $TEST_FILE:"
               # Run 'dog' on test file five
               # times
for (( i = 1; i <= 5; i++)); do
               # Identify run number
   echo -n -e "$i)\t "
               # Run 'dog' on test file,
               # Suppress 'dog' display,
               # and record run-times
   { time dog $TEST_FILE > /dev/null 2>&1 ; } 2>> dogtime.txt
               # Use this line to display
               # 'dog' output
#   { time dog $TEST_FILE ; } 2>> dogtime.txt
               # Display run-time
   dog dogtime.txt | tail -n 1
done
echo
               # Start count of recorded
               # run-times
let -i COUNT=1
               # Start run-time total at zero,
               # sending value to 'bc' for
               # decimal numbers
DOG_TOTAL=$(echo "0" | bc)
               # Signal start of summary
echo "total time for 'dog':"
               # Read each run-time in
               # recorded list
for TIME in $(dog dogtime.txt); do
               # Convert recorded times to
               # decimal numbers with 'bc'
   DECT=$(echo "$TIME" | bc)
               # Display times and decimal
               # values
   echo -e "$COUNT) TIME: $TIME\t DECT: $DECT"
               # Use 'bc' to calculate the sum
               # of run-times
   DOG_TOTAL=$(echo "$DOG_TOTAL+$DECT" | bc)
   let COUNT=$COUNT+1
done
               # Display run-time total,
               # '$DOG_TIME'
echo -e "\t\tTOTAL: $DOG_TOTAL"
echo

### Results
               # Decide which program had
               # shorter total run time
               # Display total time
               # difference, if any
if [ $(echo "$CAT_TOTAL<$DOG_TOTAL" | bc) = 1 ]; then
   DIFF=$(echo "$DOG_TOTAL-$CAT_TOTAL" | bc)
   echo "'cat' took $DIFF seconds less time than 'dog'"
elif [ $(echo "$DOG_TOTAL<$CAT_TOTAL" | bc) = 1 ]; then
   DIFF=$(echo "$CAT_TOTAL-$DOG_TOTAL" | bc)
   echo "'dog' took $DIFF seconds less time than 'cat'"
else
   echo "'cat' and 'dog' took the same time"
fi

               # Reset default 'time' format
TIMEFORMAT=$'\nreal\t%3lR\nuser\t%3lU\nsys\t%3lS'
               # Signal end of program
echo "done"; echo
exit 0


Although this comparison between 'dog' and 'cat' seems somewhat... what?... trivial?... I can see how the test itself could be useful elsewhere.

G O Free =:)


Last edited by gofree on Mon Feb 01, 2010 3:20 am, edited 1 time in total.

Top
 Profile  
 PostPosted: Mon Feb 01, 2010 1:57 am   
User avatar

Joined: Mon Jan 18, 2010 8:10 pm
Posts: 40
Here is a sample of the output:
Code:
geoffrey@Presario-Laptop:~/bin$ speed_test pickcheck

speed_test: 'cat' vs 'dog'
TEST_FILE: pickcheck

CAT
time cat pickcheck:
1)    0.003
2)    0.003
3)    0.002
4)    0.002
5)    0.002

total time for 'cat':
1) TIME: 0.003    DECT: .003
2) TIME: 0.003    DECT: .003
3) TIME: 0.002    DECT: .002
4) TIME: 0.002    DECT: .002
5) TIME: 0.002    DECT: .002
      TOTAL: .012

DOG
time dog pickcheck:
1)    0.002
2)    0.002
3)    0.002
4)    0.002
5)    0.002

total time for 'dog':
1) TIME: 0.002    DECT: .002
2) TIME: 0.002    DECT: .002
3) TIME: 0.002    DECT: .002
4) TIME: 0.002    DECT: .002
5) TIME: 0.002    DECT: .002
      TOTAL: .010

'dog' took .002 seconds less time than 'cat'
done


Here the test file is small and the times taken by each program are so minute that the difference is hardly noticeable.

As a proof of concept (exercise) however, the script seems to work fine. :)


Last edited by gofree on Mon Feb 01, 2010 3:14 am, edited 1 time in total.

Top
 Profile  
 PostPosted: Mon Feb 01, 2010 3:00 am   
User avatar

Joined: Mon Jan 18, 2010 8:10 pm
Posts: 40
gofree wrote:
...the difference is hardly noticeable.


"Infinitesimal" really... :-B

Of course, the test should be more illuminating with larger test files or more complex operations (and better statistical analysis).

g


Top
 Profile  
 PostPosted: Mon Feb 01, 2010 3:52 am   
User avatar

Joined: Mon Jan 18, 2010 8:10 pm
Posts: 40
Here, I copied '/var/log/syslog.1' to my home desktop and ran 'speed_test' on it... three times...

Code:
geoffrey@Presario-Laptop:~/Desktop$ speed_test syslog.1

speed_test: 'cat' vs 'dog'
TEST_FILE: syslog.1

CAT
time cat syslog.1:
1)    0.004
2)    0.004
3)    0.003
4)    0.003
5)    0.004

total time for 'cat':
1) TIME: 0.004    DECT: .004
2) TIME: 0.004    DECT: .004
3) TIME: 0.003    DECT: .003
4) TIME: 0.003    DECT: .003
5) TIME: 0.004    DECT: .004
      TOTAL: .018

DOG
time dog syslog.1:
1)    0.018
2)    0.007
3)    0.006
4)    0.006
5)    0.007

total time for 'dog':
1) TIME: 0.018    DECT: .018
2) TIME: 0.007    DECT: .007
3) TIME: 0.006    DECT: .006
4) TIME: 0.006    DECT: .006
5) TIME: 0.007    DECT: .007
      TOTAL: .044

'cat' took .026 seconds less time than 'dog'
done

geoffrey@Presario-Laptop:~/Desktop$ speed_test syslog.1

speed_test: 'cat' vs 'dog'
TEST_FILE: syslog.1

CAT
time cat syslog.1:
1)    0.005
2)    0.004
3)    0.003
4)    0.004
5)    0.004

total time for 'cat':
1) TIME: 0.005    DECT: .005
2) TIME: 0.004    DECT: .004
3) TIME: 0.003    DECT: .003
4) TIME: 0.004    DECT: .004
5) TIME: 0.004    DECT: .004
      TOTAL: .020

DOG
time dog syslog.1:
1)    0.007
2)    0.006
3)    0.007
4)    0.018
5)    0.006

total time for 'dog':
1) TIME: 0.007    DECT: .007
2) TIME: 0.006    DECT: .006
3) TIME: 0.007    DECT: .007
4) TIME: 0.018    DECT: .018
5) TIME: 0.006    DECT: .006
      TOTAL: .044

'cat' took .024 seconds less time than 'dog'
done

geoffrey@Presario-Laptop:~/Desktop$ speed_test syslog.1

speed_test: 'cat' vs 'dog'
TEST_FILE: syslog.1

CAT
time cat syslog.1:
1)    0.004
2)    0.004
3)    0.003
4)    0.004
5)    0.005

total time for 'cat':
1) TIME: 0.004    DECT: .004
2) TIME: 0.004    DECT: .004
3) TIME: 0.003    DECT: .003
4) TIME: 0.004    DECT: .004
5) TIME: 0.005    DECT: .005
      TOTAL: .020

DOG
time dog syslog.1:
1)    0.007
2)    0.007
3)    0.007
4)    0.006
5)    0.007

total time for 'dog':
1) TIME: 0.007    DECT: .007
2) TIME: 0.007    DECT: .007
3) TIME: 0.007    DECT: .007
4) TIME: 0.006    DECT: .006
5) TIME: 0.007    DECT: .007
      TOTAL: .034

'cat' took .014 seconds less time than 'dog'
done


In all three trials, 'cat' was faster than 'dog'--by 0.026 seconds in the first test, by 0.024 seconds in the second, and by 0.14 seconds in the third.

So, while these tests were in no way statistically conclusive, the GNU 'cat' program seems to be slightly faster the 'dog' program from the Photodex Corporation.

As 'dog' is billed as being "better than cat", it is very likely that its appeal lies in something other that sheer speed of display (even as this simple test is merely suggestive).

Perhaps it is for the companionship... :)


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 4 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP