Dear Monks,
Is there a way to measure the cpu time of a subroutine within an individual thread? I'm using v5.8.5 built for darwin-thread-multi, and the newer "threads".
I've tried:
1) setitimer then getimer (with ITIMER_PROF (and ITIMER_VIRTUAL, for kicks))
2) Benchmark's timethis()
3) times()
4) Time::HiRes' gettimeofday, etc.

No matter the method tried, when I increase the number of threads, the reported time increases dramatically as well or at least is very unstable. The simplest solution, of course, is to time the subroutine in a non-threaded environment, but I wonder if timing within a thread is possible.

Thank you very much!
#!/usr/bin/perl use Time::HiRes qw( gettimeofday tv_interval setitimer getitimer); use threads; for ($i = 0; $i < 10; $i++){ my $thr = threads->new(\&time_thread_test); } sub time_thread_test{ #various tests commented out. Sorry for the mess! #my $t0 = [gettimeofday]; #setitimer('ITIMER_PROF', 30); #timethis( 1000, sub { my $count = 0; for ($i = 0; $i <= 10000; $i+ ++){$count++;}}); my ($user,$system,$cuser,$csystem) = times; print "u: $user, s: $system, cu: $cuser, cs: $cystem\n"; my $count = 0; for ($i = 0; $i <= 10000; $i++){$count++;} # my $elapsed = tv_interval ( $t0 ); # my $elapsed = 30 - getitimer('ITIMER_PROF'); # print "$count: $elapsed\n"; print "2u: $user, s: $system, cu: $cuser, cs: $cystem\n"; }

In reply to cpu time within a thread by tallison

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.