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

Jquery and servlet communication with ajax
Replacing a parent XML tag if contains a string using regex
Linked List School Assignment
Getting Null pointer exception during String comparison
Clearing input buffer when using a Scanner
REST call :Connection timed out
Gradle and Lwjgl 3 Natives
How to add an image to an already drawn box using LWJGL & Slick2D
My program seems to be ignoring my compareTo() I wrote
Sax Parser No method found to parse
Permission Denial when trying to access contacts in Android
Guava eventbus dispatching error
Having trouble making circle calculator
Understanding how variable point to value in Java
Scala symbol to java enum
Draw a fence in Java using for loops

Categories

HOME
vim
cloud
isabelle
programming-languages
elm
malloc
slick-slider
primary-key
aurigma
google-pagespeed
jprofiler
conemu
cloudhub
format-specifiers
wijmo
crystal-reports-2010
one-to-many
jaxb2
objectanimator
quote
http-status-code-503
copying
caret
framemaker
repo
force-layout
html5-fullscreen
c11
az-application-insights
ws-security
texmaker
password-encryption
rainbowtable
mu
idisposable
yii2-extension
elgg
ifs
qtwebview
cudafy.net
imanage
smart-table
ado.net-entity-data-model
file-writing
mongocsharpdriver
lua-5.1
master-slave
nessus
rtbkit
spim
xna-4.0
php-ci
jsapi
lustre
dd
gulp-less
google-reader
ember-charts
stxxl
prettify
cdc
dir
chronoforms
plasma
browser-detection
twrequest
getmessage
pydot
infobox
invite
celltable
locate
coda-slider
gamma
noscript
mediarss

Resources

Encrypt Message



code
soft
python
ios
c
html
jquery
cloud
mobile