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

User failed to be authenticated in WebLogic
Simple Android Application not working
Second Program Window
Java program shutdown on Linux
implementing drag and drop for shapes JavaFX
Android java.net.SocketTimeoutException: Connection timed out
GATE Embedded sample Example NoClassFound Error
Mockito: Mock object based on another
Getting phrases which have each word starting with an uppercase character
Jruby check if JavaMail headers contains a specific name
Debugging java source file with multiple classes in eclipse: Source not found
Java 7 - LinkOption - why is NOFOLLOW_LINKS the only available option?
Multiple Entrypoints in Struts Action (Migration Struts 2.2.3 -> 2.3.1)
JOptionPane display loop
How would I go about looping back to the beginning?
Event fired only once when watching a directory

Categories

HOME
pdf
date
gerrit
reflection
office365api
alfresco
twitter-bootstrap-4
win32gui
fsm
angular-ui-bootstrap
multiple-records
slurm
gitpitch
elasticsearch-hadoop
izpack
zend-framework3
visual-studio-2005
designer
lombok
uitypeeditor
opentracing
ghc
progressive-web-apps
tostring
windows-7-x64
contextmenu
lldb
zapier
mustache.php
kudan
hammerspoon
angular2-aot
opennlp
facebook-access-token
traffic
tooltipster
entitlements
minitab
buck
opentype
atlassian-crucible
instant-messaging
socialengine
google-qpx-express-api
suricata
objectlistview
impersonation
webdriver-manager
cookiecutter-django
unoconv
avro4s
ruby-on-rails-3.1
qcombobox
wptoolkit
babel-core
outlook-2013
rainbowtable
smartcontracts
logparser
idisposable
dtexec
sybase-asa
mako
spring-cache
parallel-data-warehouse
savon
skobbler-maps
nsfilemanager
websitepanel
essence
captivenetwork
pyke
xna-4.0
lemon
marching-cubes
feedback
kcachegrind
responsive-images
oberon
unity5.2.3
cdt
fpml
gulp-less
castle
heisenbug
contenttype
rtmfp
app42
algebraic-data-types
commoncrypto
dataservice
html-editor
simba
first-responder
tridion-worldserver
work-stealing
boost-filesystem
data-loss
servicehost
actionview
sudzc
modelstate
exchange-server-2003
procedural-music
grid-system
3gp
uiq3

Resources

Encrypt Message