What is the best way to record a process execution time?

This is a discussion on What is the best way to record a process execution time? within the Linux Programming forums, part of the Platform Specific Boards category; I need to measure a process execution time in my own shell program. Currently, I am research on the "time" ...

  1. #1
    Registered User
    Join Date
    Mar 2006
    Posts
    2

    Question What is the best way to record a process execution time?

    I need to measure a process execution time in my own shell program.

    Currently, I am research on the "time" command provides by unix system.

    At the terminal, I typed the command "time ls", where "ls" is the process i want to execute and measure for it run time.

    The command execute as below:

    $time ls

    real 0m0.007s
    user 0m0.000s
    system 0m0.000s

    This result is send to the standard output and print on screen. However, I need it to be store in a file instead.

    I tried the command "time ls >> abc.txt", but it store the execution result of "ls" instead of "time ls" into the file.

    I tried "time -o abc.txt ls" but the system say -o command not found.

    Anyone who know how to direct the "time" result" to a file instead on printing on the screen? Or are there a better way to record the execution time for a process?

    Please advise me! Thanks in advance ^^

  2. #2
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,077
    I have very, very little experience in UNIX and am probably wrong, but I'll wager a guess for you to try. Or you can just flat out tell me it's ridiculous and it would never work without having to try it.

    Try piping LS into the time command and sending that to a file.
    Code:
    ls | time > abc.txt
    Again, I have no idea if this would do anything, but I just got a gut feeling and I'm not sure if it's this answer or that roast beef I had earlier.

    EDIT: Hmm... now it's more of an bladder feeling. I'm thinking it's the roast beef. Also I don't think piping ls to time would do anything. Nyuck nyuck. Thinking about how pipes work I don't see why what you put in your original post wouldn't work. Oh well. Time to grab a magazine and... I'll stop there.

    Also, as a more proper guess... maybe try the '>' pipe instead of the '>>'
    Last edited by SlyMaelstrom; 03-15-2006 at 01:00 AM.
    Sent from my iPad«

  3. #3
    Mad OnionKnight's Avatar
    Join Date
    Jan 2005
    Location
    Umeň, Sweden
    Posts
    555
    This doesn't to be the right place to ask this question. You'll probably get more answer at linuxquestions.org or similar.

    I haven't used time much and I fiddled around with it and got the same problems like you are getting. 'man time' tells me that it's writing to stderr but that doesn't seem to be the case either because redirecting it won't change a thing.

  4. #4
    Registered User
    Join Date
    Mar 2006
    Posts
    2
    to SlyMaelstrom:

    I tried your suggestion. However, ls|time abc.txt is not a valid command as the system expect time [option] and thus the abc.txt can't be write after it.

    Anyway, still thanks for your reply, hope you will enjoy for the magazine ;p



    to OnionKnight:
    Thanks, I just reliaze that this is a unix system question instead of C prog... lol

  5. #5
    cwr
    cwr is offline
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    Using bash/sh:

    Discard the output of ls and store the result of time in the file:
    Code:
    ((time ls >/dev/null) 2>&1) >> file
    Store both the ls output and the result of the time in the file:
    Code:
    ((time ls) 2>&1) >> file

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,821
    A bit of shell magic
    Code:
    $ ( time ls > files.txt ) 2> times.txt
    
    $ cat files.txt times.txt
    a.exe
    files.txt
    foo.c
    foo.cpp
    foo.s
    iFolder
    times.txt
    tmp1.txt
    tmp2.txt
    
    real    0m0.078s
    user    0m0.046s
    sys     0m0.030s
    The () create a sub-shell to do all the work
    2> means redirect stderr (where time prints its results) to a file.

    EDIT: Gah - beaten
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  7. #7
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,077
    How come "times.txt" was in the directory? Wouldn't it have not existed at the time when ls was piped to files.txt? Had you done this previous to the example you showed meaning it existed already or am I just missing something here?

    Edit: My first post in Linux Programming. Man, I really have no place in this part of the forums.
    Sent from my iPad«

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,821
    > How come "times.txt" was in the directory?
    Yes and Yes
    Yes I'd done it before and Yes you're missing something
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with binary file c++
    By lucky_mutani in forum C++ Programming
    Replies: 4
    Last Post: 06-05-2009, 10:24 AM
  2. How to get program execution time
    By pobri19 in forum C++ Programming
    Replies: 8
    Last Post: 01-20-2009, 07:45 AM
  3. Execution Time in Microseconds?
    By thetinman in forum C++ Programming
    Replies: 11
    Last Post: 06-02-2007, 02:32 PM
  4. Read and set\change system time
    By Hexxx in forum C++ Programming
    Replies: 9
    Last Post: 01-02-2006, 07:11 AM
  5. I apologize. Good bye.
    By doubleanti in forum A Brief History of Cprogramming.com
    Replies: 14
    Last Post: 05-03-2002, 07:51 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21