in reply to latest on ithreads vs forks?

Got some interesting (to me) data on this comparison. After finding a way to track memory and cpu usage for child processes, here's how threads vs forks stacks up:

threads.pm: WC: 13.76s Usr: 0.39s Sys: 0.07s CSv/f: 0/0 IOops: 0/0 Sigs: 0 Swaps: 0 PF: 2500/1265 Msg: 0/0 Me +m: 65499 forks.pm: WC: 18.30s Usr: 0.33s Sys: 0.22s CSv/f: 0/0 IOops: 0/0 Sigs: 0 Swaps: 0 PF: 14427/2524 Msg: 0/0 M +em: 5454

These come from running the exact same tests ~20 times, varying only the use of threads.pm vs forks.pm. The code for this test is below, it does do a little bit of data sharing (since my intended usage model will as well).

Interesting to note that forks.pm does use far less memory, but gets far more page faults, and also uses more cpu time. Guess it depends which is more valuable to you. As stated by others above, forks.pm isn't necessarily the ideal implementation for using forks, but rather a convenient one because it uses the threads.pm interface.

Here's the code I used. For both threads and forks, I ran 9 times with 10 threads, 6 with 20, 3 with 50, and 3 with 100.

# which threading model do we want? use threads; use threads::shared; #use forks; use forks::shared; $num_threads = shift(@ARGV) || 10; $nonshared_var = 1; my $shared_var : shared = 1; foreach (0..$num_threads-1) { # create a new thread push(@threads,threads->new(\&thread_sub)); } foreach $thread (@threads) { # wait for threads to finish $thread->join(); } printf "Orig thread done\n"; sub thread_sub { my($count) = 20*rand() + 1; my($random); printf "Thread %d started\n",threads->tid(); # do some stuff while ($count-- > 0) { $random = (5*rand()) | (time() & 0x3); $nonshared_var += int($random); lock($shared_var); $shared_var += int($random); sleep($random); printf(" Thread %d loop %d: random=%d, nonshared_var=%d, ". "shared_var=%d\n",threads->tid(),$count,$random,$nonsha +red_var, $shared_var); threads->yield(); } printf("Thread %d DONE!\n",threads->tid()); }

Replies are listed 'Best First'.
Re: Re: latest on ithreads vs forks?
by liz (Monsignor) on May 29, 2004 at 10:15 UTC
    If you are interested to test the memory usage of different ways of using Perl ithreads, there's of course Benchmark::Thread::Size by yours truly.

    Liz