java


Why my parallel program is slower than sequential program? [duplicate]


I've been learning a little about parallelism in the last few days, and I came across this example.
I put it side to side with a sequential for loop like this:
private static void NoParallelTest()
{
int[] nums = Enumerable.Range(0, 1000000).ToArray();
long total = 0;
var watch = Stopwatch.StartNew();
for (int i = 0; i < nums.Length; i++)
{
total += nums[i];
}
Console.WriteLine("NoParallel");
Console.WriteLine(watch.ElapsedMilliseconds);
Console.WriteLine("The total is {0}", total);
}
I was surprised to see that the NoParallel method finished way way faster than the parallel example given at the site.
I have an i5 PC.
I really thought that the Parallel method would finish faster.
Is there a reasonable explanation for this? Maybe I misunderstood something?
The sequential version was faster because the time spent doing operations on each iteration in your example is very small and there is a fairly significant overhead involved with creating and managing multiple threads.
Parallel programming only increases efficiency when each iteration is sufficiently expensive in terms of processor time.
You are not comparing apples to oranges.
The example you link to isn't about parallelism or concurrency as much as it is about disconnecting state from being shared amoung the different threads. The work it is doing is a straw man to just show that something is being done.
So what you are comparing is like comparing an F1 car to a Top Fuel Dragster. Each excel at going fast, but fast is relative to the context which in this and most cases is highly specialized. Neither can compete in the others highly specialized bailiwick.
Lessons to learn
concurrent != faster
poorly conceived benchmarks mislead
I think that's because the loop performs a very simple, very fast operation.
In the case of the non-parallel version that's all it does. But the parallel version has to invoke a delegate. Invoking a delegate is quite fast and usually you don't have to worry how often you do that. But in this extreme case, it's what makes the difference. I can easily imagine that invoking a delegate will be, say, ten times slower (or more, I have no idea what the exact ratio is) than adding a number from an array.

Related Links

What is a difference between <? super E> and <? extends E>?
Convert the following C# code into java code? [closed]
It it necessary to learn J2ME for Android or iPhone development? [closed]
JTidy upgrade broke document xpaths
Integrate applications using multiple programming languages into a Java application
java ResultSet, using MAX sql function
How do I get parameter from URL or hidden field
Java thread: 'join' froze my program
Is it possible to change the base millisecond reference time from 1970 to 2008 in Java (like JSR-310)
configuring applet options in the <applet> tag
How to invoke the getView method in the baseAdapter in Android from another WebService Bean?
Stuck on emulator screen in Eclipse with Android Development?
Image Uploading in a web aplication [closed]
Java using generics on Map with two types
Grails Deployment - Fastest way to get deployed?
Profiling a Java Spring application

Categories

HOME
java
pandas
multithreading
bluetooth
netsuite
drivers
pycharm
plot
read-eval-print-loop
q
at-command
razor
c#-2.0
datastax-java-driver
cross-validation
here-api
resize
decimal
seaborn
reactive-cocoa
grails3
jasonette
physics-engine
graphicsmagick
dcevm
kvc
kudan
telerik-reporting
blazemeter
crystal-reports-2010
url-scheme
intel-pin
spring-mybatis
unboundid
nat
javascriptcore
oracle-fusion-middleware
copying
framemaker
google-api-nodejs-client
normal-distribution
picasso
xcode-extension
espeak
occlusion
unobtrusive-validation
preconditions
estimote
heightmap
jsch
theming
hendrix
celery-task
idisposable
nativeapplication
flutterwave
pintos
bstr
multiple-regression
superstack
master-slave
bgp
pickadate
yaws
xpath-1.0
spim
android-listview
tableau-online
myo
vstest.console.exe
ami
geonetwork
ng-animate
angularjs-ng-click
mdt
code-access-security
responsive-slides
kgdb
app42
dir
inbox
dataadapter
limejs
tinn-r
funscript
dice
mercurial-server
f#-powerpack
calling-convention
inotifycollectionchanged
radcombobox
bespin
fixed-width
brewmp
yagni
project-hosting
caching-application-block

Resources

Database Users
RDBMS discuss
Database Dev&Adm
javascript
java
csharp
php
android
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App