tallison has asked for the wisdom of the Perl Monks concerning the following question:

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"; }

Replies are listed 'Best First'.
Re: cpu time within a thread
by BrowserUk (Patriarch) on Jul 28, 2004 at 17:34 UTC
    No matter the method tried, when I increase the number of threads, the reported time increases dramatically...

    Assuming that your running on a single cpu machine, that is to be expected.

    Using two threads will not double the capacity of your cpu. They shared the cpu. Both threads will get (slightly less than) half of the time a single thread would get. All other things being equal.

    Threads are not a way of making your code run faster. The main purpose to is allow one part of your program to continue doing something useful whilst another part of your program is waiting for something to happen--usually IO of some form. Waiting for the user to type something, or a webserver to respond to a query etc.

    There are other uses, but the limited api that Perl threads present of the underlying mechanisms restricts many of these.


    Examine what is said, not who speaks.
    "Efficiency is intelligent laziness." -David Dunham
    "Think for yourself!" - Abigail
    "Memory, processor, disk in that order on the hardware side. Algorithm, algoritm, algorithm on the code side." - tachyon
      Thanks. I understand that threads won't help (and will likely slow) clock time on a single processor, but that they are useful for I/O. The question is about cpu time.
      Theoretically, in this toy example, the cpu should spend roughly the same amount of time within each thread (no?), no matter how many threads are running.
      I'm wondering if there is a way to measure the cpu time within each thread.
      Thanks again.

        Not that I am aware of under Win32.

        I seem to recall somebody mentioning that under some versions/configurations of *nix, threads were listed in top as separate entities much like processes, but I also seem to recall that this was being superceded by a 'rolled up' view of each process. Sorry, I can't remember where I read this, it could be a figment of my imagination.

        This isn't something that can be easily done outside of the kernel. Any timing based upon the system clock simply won't account for the time when a thread is 'swapped out'.

        The only mechanism by which this might be possible is if there is an interface to the scheduler and I do not know of any such.


        Examine what is said, not who speaks.
        "Efficiency is intelligent laziness." -David Dunham
        "Think for yourself!" - Abigail
        "Memory, processor, disk in that order on the hardware side. Algorithm, algoritm, algorithm on the code side." - tachyon