Find the answer to your Linux question:
Results 1 to 3 of 3
Hi all, I'm trying to use a perl script to measure average running times of a program I wrote for school. I am using the time command, but it isn't ...
  1. #1
    Just Joined!
    Join Date
    Jun 2005
    Posts
    7

    Help w/ time measuring script

    Hi all,

    I'm trying to use a perl script to measure average running times of a program I wrote for school. I am using the time command, but it isn't working the way I expect. Here is a sample:

    #!/usr/bin/perl

    $output = `time -f %e ./myprogram`;
    print "output = $output";

    The result of this script will be something like:
    1.50
    output = .00526

    Where .00526 is the actual output of my program. In this case, I don't care about the actual output, I only care about the run time. I'm not exactly sure what is happening here, but it looks like the 1.50 is being printed out independently of being assigned to my variable, and the value assigned to my variable is actually the output of the program.

    Eventually what I want to do is put that code in a loop and have something like:

    $count = 0;
    $output = 0;
    while ($count < 30)
    {
    $output += `time -f %e ./myprogram`;
    $count++;
    }

    print ($output / 30); # in order to print my average run time here...

    Can anyone please help? I know C++ pretty well but I'm a Linux and perl newb. Thanks!

  2. #2
    Linux Enthusiast likwid's Avatar
    Join Date
    Dec 2006
    Location
    MA
    Posts
    649
    Well using -f %e on my shell doesn't work. I think that might be a csh or tcsh thing. Anyways, the actual output of the command time ./program seems to only be the stdout of program, not time. Time happens to write to stderr AFTER the execution of program. This is interesting to me so I will be looking for a solution for you, I should have it soon.

  3. #3
    Linux Enthusiast likwid's Avatar
    Join Date
    Dec 2006
    Location
    MA
    Posts
    649
    Ok so turns out you have to execute time in a subshell or a code block to get its output. So something like this works

    Code:
    (time dd if=/dev/zero of=/tmp/2mb bs=1024 count=2048) 2>&1 | grep real | cut -f2
    That command in my shell shows the real time of executing that dd string. If your shell can interpret the -f %e properly then grepping for real and cutting the second field is useless to you.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...