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

why is the constant FLAG_FULLSCREEN repeated in place of int mask in the following method?
Java: Printing only the First and Last when the names that the user gave are organized alphabetically
Youtube Player API results in java.lang.NoClassDefFoundError
How to get the path of web application's war file?
java.lang.OutOfMemoryError: PermGen space,I am using tomcat on the Mac Os
Call a child class method in a parent two levels above?
Synchronizing/transferring contacts from phone to app
Dagger doesn't like constructors that throw exceptions
Employee schedule entity relationship
Player vs. computer tic tac toe game code
How to make multiplication and division counted first in my calculator?
Eclipse ANT Build.xml create JAR is showing corrupted file
what are different ways to read specific lines from text file in java? [duplicate]
Webelement, which is defined in page class, is not accessible in Test class
java helper class for merging/filling elements in list
How to read from a text file, which is simulatenously getting written to? - JAVA

Categories

HOME
google-chrome-extension
variables
coq
magnific-popup
ngrx
google-play
tinymce
nullpointerexception
filtering
analysis
malloc
rubygems
echarts
upload
contact
synchronization
facebook-messenger-bot
amazon-cloudformation
dacpac
vault
fancybox-3
user-input
volttron
rascal
mapserver
timeout
circular-dependency
gz
flask-wtforms
after-effects
tostring
solaris-10
reactive-cocoa
beyondcompare
nodatime
lldb
nas
amazonsellercentral
trading
crosstab
su
entitlements
social-media
fifo
neo4j-spatial
wtx
bluestacks
yadcf
.net-4.0
karaf
avcapturesession
force-layout
wpf-controls
vxworks
gesture
temporary-files
optix
arena-simulation
atomicity
veracode
diagnostics
sqlclient
arrow-keys
imanage
clang-static-analyzer
prettytensor
yaws
rhino-servicebus
thredds
tmuxinator
purge
myo
method-parameters
sailfish-os
unity3d-gui
graph-api-explorer
notify
lustre
kineticjs
nstableviewcell
castle
starcluster
marmalade
ccss
dexterity
runtime.exec
odata4j
spring-validator
html-editor
funscript
cascalog
qtkit
punbb
gnu-prolog
libc++
infobox
joyent
mongomapper
asp.net-mvc-areas
mozilla-prism
microsoft-virtualization

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App