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

Where “bpm:activitiStartTask” is used in alfresco?
How to handle dates when the input dates are in the transition period from PDT to PST?
Querying to get the ObjectID of the Project through Rally Rest API in java
static map sent to worker nodes before getting populated in afterPropertiesSet()
Unable to parse element attribute with XOM
How to get Images from resources mipmap using a Java class
Java - I have an issue with Scanner class and reading text file [duplicate]
Spring REST #RequestMapping practices
Java return multiple strings in one method
How to hold a button and change its size to fullscreen
How to return a String in an inherited class?
getId() on view obtained from recyclerView.findChildViewUnder(e.getX(), e.getY()) is -1
Null handling in Java 1.7 and 1.6
Java Programming , Type Casting
RecyclerView GridLayoutManager first row with 2 items
Reading Linux Repositories from Third Party

Categories

HOME
proxy
zeromq
spring-cloud-stream
elm
v8
callback
yum
enterprise-library-5
in-app-purchase
ios-charts
volttron
static-libraries
aurigma
excel-vba-mac
shared-hosting
predix
intentfilter
css-animations
restful-authentication
claims-based-identity
dxf
firebase-crash-reporting
crosstab
emgucv
chromebook
log4js-node
windows-server-2000
primitive
fog
calibre
perlin-noise
ios5
file-format
webdriver-manager
mesos-chronos
node-gyp
isbn
node-sass
thin
websphere-mq-fte
qwt
scrollable
jquery-nestable
sage-one
drupal-6
worker-thread
sqlclient
crypt
nsarray
spring-mongodb
abcpdf9
jquery-filter
url-pattern
deadbolt-2
azure-virtual-network
login-control
measures
mathematica-frontend
plottable.js
captivenetwork
browser-bugs
vstest.console.exe
typekit
tablelayout
elliptic-curve
htmlcleaner
bundles
cloud-connect
typo3-neos
openlaszlo
html5-notifications
floating-point-precision
gridfs
quartz-graphics
hungarian-algorithm
angularjs-controller
nsdatecomponents
django-tagging
xmemcached
self-extracting
netbeans-7.1
ecl
celltable
datareader
xsdobjectgen
filtered-index
fixed-width
swing-app-framework
text-coloring
thread-local-storage
gacutil

Resources

Encrypt Message



code
soft
python
ios
c
html
jquery
cloud
mobile