in reply to Re: Wanting some clarification / opinions on MCE vs Threads
in thread Wanting some clarification / opinions on MCE vs Threads
I haven't seen any direct comparisons between the two but I think MCE::Grep and MCE::Map are two examples of MCE code that is more readable than the threads equivalent (I should say my threads equivalent)
versususe MCE::Grep; my @a = mce_grep { $_ % 5 == 0 } 1..10000;
use threads; use Thread::Queue; my @thread_pool; my $q = Thread::Queue->new(); my $results = Thread::Queue->new(); for (0..10000) { $q->enqueue($_); } for (0..1) { push @thread_pool, threads->create( \&grep ); } sub grep { while (my $work = $q->dequeue() ) { if ( $work % 5 == 0 ) { $results->enqueue($work); } } $q->enqueue(undef); } map {$_->join(); } (@thread_pool); $results->enqueue(undef); my @results; while ( my $result = $results->dequeue() ) { print $result, "\n"; push @results, $result; }
I'm sure that the threads version could be done much more easily than I hacked together in 5 minutes. It could just be that how I write using threads is just poor. Regardless, I don't think there is a threads implementation as simple as the MCE version. Additionally, this is also a special case where MCE has a built-in function that provides this functionality, but there are similar constructs for most of the simple cases. For what I do there isn't much that can't be implemented using some mixture of MCE::Grep, MCE::Map and MCE::Loop so I'm biased.
I should also note that I haven't written very much (no "production" code as it were) using MCE so I may not have encountered some of its limitations compared to threads.
|
|---|