LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-28-2010, 09:00 PM   #1
BrianK
Senior Member
 
Registered: Mar 2002
Location: Los Angeles, CA
Distribution: Debian, Ubuntu
Posts: 1,334

Rep: Reputation: 51
tcsh script: can you time parts of the script?


In lots of different languages, you can do a kind of stop watch around a function or group of lines to see how long it/they takes.

Can you do something like that in tcsh? I need to be able to time in at least tenths of a second & preferably hundredths. What I'm trying to do is something like this:

Code:
#!/bin/tcsh -f

`do_some_stuff`

start = date

foreach f in (`foo`)
  do_something
end

end = date

elapsed = end - start

echo "It took $elapsed seconds"
Is something like this possible?

I don't want to "time" the whole script because I want to time several parts of the script to see where the slowness is.
 
Old 01-29-2010, 03:37 PM   #2
rweaver
Senior Member
 
Registered: Dec 2008
Location: Louisville, OH
Distribution: Debian, CentOS, Slackware, RHEL, Gentoo
Posts: 1,833

Rep: Reputation: 167Reputation: 167
Date can display the current nanoseconds also which would be more useful for timing...

Code:
core:~$ date "+%F %T.%N"
2010-01-29 16:35:20.240816070
core:~$
or (easier to start with seconds & nanoseconds)

Code:
core:~$ date "+%s.%N"
1264801049.611436547
core:~$

Last edited by rweaver; 01-29-2010 at 04:06 PM.
 
Old 01-29-2010, 03:50 PM   #3
BrianK
Senior Member
 
Registered: Mar 2002
Location: Los Angeles, CA
Distribution: Debian, Ubuntu
Posts: 1,334

Original Poster
Rep: Reputation: 51
Ahh nice. I didn't know date had that much resolution. I wrote a little C program to do it for me, but I like using date better.

Thanks!
 
Old 01-29-2010, 05:30 PM   #4
tuxdev
Senior Member
 
Registered: Jul 2005
Distribution: Slackware
Posts: 2,012

Rep: Reputation: 115Reputation: 115
nanosecond resolution for timing is completely worthless. Variation due to scheduling overhead is far less precise, on the order of tenths of seconds. Use the "time" command.

The first mistake, though, is scripting in (t)csh.
http://www.faqs.org/faqs/unix-faq/shell/csh-whynot/

Last edited by tuxdev; 01-29-2010 at 05:40 PM.
 
Old 01-29-2010, 06:32 PM   #5
BrianK
Senior Member
 
Registered: Mar 2002
Location: Los Angeles, CA
Distribution: Debian, Ubuntu
Posts: 1,334

Original Poster
Rep: Reputation: 51
Quote:
Originally Posted by tuxdev View Post
nanosecond resolution for timing is completely worthless. Variation due to scheduling overhead is far less precise, on the order of tenths of seconds. Use the "time" command.
I don't think you can use time from within a script, though, can you? IN other words, can I run time around 3 loops within a script to see how long each loop takes?
 
Old 02-01-2010, 03:59 PM   #6
rweaver
Senior Member
 
Registered: Dec 2008
Location: Louisville, OH
Distribution: Debian, CentOS, Slackware, RHEL, Gentoo
Posts: 1,833

Rep: Reputation: 167Reputation: 167
You could use time to call separate scripts to time their output, and it also work on loops and individual as well as commands (of course) eg:

Code:
time for i in `ls`; do echo $i; done
time ls -al
 
Old 02-02-2010, 03:49 PM   #7
makyo
Member
 
Registered: Aug 2006
Location: Saint Paul, MN, USA
Distribution: {Free,Open}BSD, CentOS, Debian, Fedora, Solaris, SuSE
Posts: 735

Rep: Reputation: 76
Hi.

If you are using tcsh:
Code:
       time    If set to a number, then the time builtin (q.v.) executes auto-
               matically after each command which takes more  than  that  many
               CPU seconds. 

-- excerpt from man tcsh
For example, using a local program burn to use an approximate amount of CPU time:
Code:
#!/usr/bin/env tcsh

# @(#) s1	Demonstrate tcsh automatic timing of simple commands.

echo
setenv LC_ALL C ; setenv LANG C
echo "Environment: LC_ALL = $LC_ALL, LANG = $LANG"
echo "(Versions displayed with local utility version)"
sh -c "version >/dev/null 2>&1" && version "=o" tcsh
echo

set time = 1
echo " Time set to $time"
echo " Burning for 2"
burn 2

echo
echo " Time for burn of 3"
burn 3

echo
exit 0
producing:
Code:
% ./s1

Environment: LC_ALL = C, LANG = C
(Versions displayed with local utility version)
OS, ker|rel, machine: Linux, 2.6.26-2-amd64, x86_64
Distribution        : Debian GNU/Linux 5.0 
tcsh 6.14.00

 Time set to 1
 Burning for 2
calibration end = 0.22, start = 0
Iterations = 50000, time = 0.22 seconds.
Iterations = 404545.454545455, time = 1.84 seconds.
2.060u 0.008s 0:02.13 96.7%	0+0k 0+0io 0pf+0w

 Time for burn of 3
calibration end = 0.22, start = 0
Iterations = 50000, time = 0.22 seconds.
Iterations = 631818.181818182, time = 2.88 seconds.
3.104u 0.004s 0:03.82 81.1%	0+0k 0+0io 0pf+0w
See man tcsh for details ... cheers, makyo
 
Old 02-02-2010, 07:20 PM   #8
BrianK
Senior Member
 
Registered: Mar 2002
Location: Los Angeles, CA
Distribution: Debian, Ubuntu
Posts: 1,334

Original Poster
Rep: Reputation: 51
Thanks for the additional suggestions. I'm getting back to this today, so I'll give 'em a try.

 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
convert tcsh script to the bash one fazanpai Linux - Software 2 11-29-2009 08:10 AM
Need help with tcsh script!!!! rallen22 Linux - Newbie 2 11-04-2009 03:32 PM
tcsh script problem nanxy Programming 4 08-19-2008 12:35 PM
tcsh shell script assignment problem BrianK Programming 4 01-18-2008 10:32 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:58 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration