A little while ago I wrote a simulation of the Monty Hall puzzle.  The 
simulator is given 3 players of different strategies (one makes a decision at 
random, the other never switches doors, and the third always switches) and runs 
each player through a configurable number of games, outputting each player's 
outcomes at the end.  Seems fairly simple and everything was working as 
expected.

Seeing as I have a dual core machine (XP Pro, JRE 1.6_10), I looked at the way 
the CPU was being utilized and to my surprise I saw a fairly even utilization 
of both cores, despite the program being single-threaded.  Then I decided to 
make the simulator multi-threaded and see what would happen to the utilization 
graph and speed of the application.  I converted the
 code invoking the game generation and running API to use Java concurrency 
framework, which also game me good control over how many threads I wanted to 
use or if I wanted to run everything single-threaded still.  Once I got 
everything working again, I was dismayed to see somewhat uneven invocation of 
the cores and, more importantly, the multi-threaded mode seems to be running 
consistently slower than the single-threaded mode.  It also uses more memory, 
which appears to be directly proportional to how many threads I allow it to 
allocate.  The memory demands make sense, but what's the deal with worse 
performance in concurrent mode?  Here is a sample output:

Tries: 1000000.
Single-threaded...
Random Player won 500177 cars and 499823 goats.
Cars were distributed as follows:       332820  333805  333375
Goats were distributed as follows:      667180  666195  666625

Never Change Player won 333555 cars and 666445 goats.
Cars were distributed as follows:       333274  333294  333432
Goats were distributed as follows:      666726  666706  666568

Always Change Player won 667476 cars and 332524 goats.
Cars were distributed as follows:       332660  333696  333644
Goats were distributed as follows:      667340  666304  666356

Execution time: 28sec.

Concurrent...
Random Player won 499379 cars and 500621 goats.
Cars were distributed as follows:       333939  333019  333042
Goats were distributed as follows:      666061  666981  666958

Never Change Player won 333384 cars and 666616 goats.
Cars were distributed as follows:       333502  331961  334537
Goats were distributed as follows:      666498  668039  665463

Always Change Player won 666691 cars and 333309 goats.
Cars were distributed as follows:       334080  332585  333335
Goats were distributed as follows:      665920  667415  666665

Execution time: 38sec.

I'm using a separate random number generator for each game play, so that at 
least shouldn't be the bottle neck according to the Javadocs.  I'd be happy to 
post the source if need be, but I'm hoping I'm missing something simple.

Alexey
2001 Honda CBR600F4i (CCS)
1992 Kawasaki EX500
http://azinger.blogspot.com
http://bsheet.sourceforge.net
http://wcollage.sourceforge.net



      
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "The 
Java Posse" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/javaposse?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to