For Programmers: Free Programming Magazines  


Home > Archive > Tcl > April 2007 > Trying to do a simple time command for procs...can't get output into console...









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Trying to do a simple time command for procs...can't get output into console...
daneyul

2007-04-27, 10:06 pm


Hi,

Does anyone have a good generic method of easily timing every proc in
an application for diagnostics? Something that can be turned on and
off at will by simply redefining the proc command? I'm trying from a
freewrapped app, using the following at the beginning of the script,
but I'm failing miserably...

proc TimeProcs {} {
console show
rename ::proc ::proc2
::proc2 ::proc {name vars body} {
::proc2 $name $vars "time {$body}"
}
}

While this doesn't fail, and all the procedures in the script
function, I can't come up with a way to put the time output into the
console.

Inserting a puts command before the time command in the new proc2
procedure gets me into quoting hell. No method seems to work here for
actually getting the output into the console.

Any help would be appreciated!

-Danny

Cameron Laird

2007-04-27, 10:06 pm

In article <1177711837.107031.156720@o40g2000prh.googlegroups.com>,
daneyul <danielb@colorbytesoftware.com> wrote:
>
>Hi,
>
>Does anyone have a good generic method of easily timing every proc in
>an application for diagnostics? Something that can be turned on and

Glenn Jackman

2007-04-28, 7:06 pm

At 2007-04-27 06:10PM, "daneyul" wrote:
>
> Hi,
>
> Does anyone have a good generic method of easily timing every proc in
> an application for diagnostics? Something that can be turned on and
> off at will by simply redefining the proc command? I'm trying from a
> freewrapped app, using the following at the beginning of the script,
> but I'm failing miserably...
>
> proc TimeProcs {} {
> console show


In case you're using this proc outside of Tk:
catch {console show}

> rename ::proc ::proc2
> ::proc2 ::proc {name vars body} {
> ::proc2 $name $vars "time {$body}"
> }


After some experimentation, I came up with:
::proc2 ::proc {name vars body} {
::proc2 $name $vars [format {puts [time [list %s]]} $body]
}

Testing:
% proc test {} {for {set i 0} {$i < 5} {incr i} {puts $i; after 1000}}
% test
0
1
2
3
4
5073353 microseconds per iteration

--
Glenn Jackman
"You can only be young once. But you can always be immature." -- Dave Barry
Glenn Jackman

2007-04-28, 7:06 pm

At 2007-04-27 06:10PM, "daneyul" wrote:
>
> Hi,
>
> Does anyone have a good generic method of easily timing every proc in
> an application for diagnostics? Something that can be turned on and
> off at will by simply redefining the proc command? I'm trying from a
> freewrapped app, using the following at the beginning of the script,
> but I'm failing miserably...
>
> proc TimeProcs {} {
> console show


In case you're using this proc outside of Tk:
catch {console show}

> rename ::proc ::proc2
> ::proc2 ::proc {name vars body} {
> ::proc2 $name $vars "time {$body}"
> }


After some experimentation, I came up with:
::proc2 ::proc {name vars body} {
::proc2 $name $vars [format {puts [time {%s}]} $body]
}

Testing:
% proc test {} {for {set i 0} {$i < 5} {incr i} {puts $i; after 1000}}
% test
0
1
2
3
4
5073353 microseconds per iteration

--
Glenn Jackman
"You can only be young once. But you can always be immature." -- Dave Barry
Alexandre Ferrieux

2007-04-28, 7:06 pm

On Apr 28, 12:10 am, daneyul <dani...@colorbytesoftware.com> wrote:
> Inserting a puts command before the time command in the new proc2
> procedure gets me into quoting hell. No method seems to work here for
> actually getting the output into the console.
>
> Any help would be appreciated!


Do you realize that when you instrument a proc, you have to preserve
its return value (and potentially exceptions it raises) ?
The [time] function returns a string like "xxx microseconds by
iteration". So at the very least to preserve the return value you must
do something like:

proc instrumented_f args {
puts stderr "TIME(f): [time {set _res [eval f $args]}]"
return $_res
}

Of course this is not really transparent since it introduces an extra
call frame. A better approach would directly call the body of f
instead of calling f. But this is not trivial (implies [catch] to
handle various [return]'s from the middle of thebody, and exceptions).
Your best bet would be to follow Cameron's advice rather than reinvent
a somewhat complicated wheel.

-Alex

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com