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

Return statement in Java methods
Java string: character replacing in sentence
org.json.JSONException: JSONArray[20] not found
How java references work when passing method parameters [duplicate]
Collections.swap() in java swap everything in the method
Why there's a eclipse/netbeans version for Windows if java “run everywhere”
how to convert seconds into hhmmss
converting audio file in java
Trying to create a List of user-defined objects, getting Object List
Internal cast of collection elements
Soap: How to validate empty elements
share video mp4 using pending intent notification
Can I have a field in a #Document that is of the Class type?
Calling constructor of superclass doesn't work
connect MYSQL with netbeans 7.4
How to Continue R.raw.audio sound after completing one loop by clicking on button in android

Categories

HOME
ms-access
nullpointerexception
office365api
session
relayjs
framework7
enterprise-library-5
autotools
synchronization
basic
offline
quicklook
remote-access
openrefine
serilog
http-status-code-504
oxyplot
crystal-reports-2008
jprofiler
emulator
dcevm
language-agnostic
java-7
sparse-matrix
poltergeist
intel-pin
pingfederate
bootstrap-material-design
saas
facebook-apps
libuv
libssl
file-rename
srcset
ioio
catel
caret
azure-sql-database
wpf-controls
libraries
android-browser
sharefile
xenforo
http-live-streaming
turbogears
google-datalayer
jsch
azure-application-gateway
boost-multi-index
epson
url-pattern
google-feed-api
fody
trash
django-debug-toolbar
fadeout
livequery
tform
maven-tomcat-plugin
intellij-14
tmuxinator
content-length
sframe
sortedlist
pretty-print
device-orientation
csquery
python-green
facebook-graph-api-v2.4
cdt
asp.net-web-api-odata
titanium-modules
castle
openexr
typeof
wp-query
marmalade
undefined-reference
didselectrowatindexpath
industrial
soundtouch
ruby-datamapper
ora-00911
buster.js
mylyn
html-editor
libstdc++
bluepill
first-responder
code-cleanup
nsdatecomponents
qt-jambi
xmlspy
f#-powerpack
android-sdk-2.1
gallio
gamequery
web-architecture
nintendo-ds
putchar
defensive-programming
msdev
mdac
data-acquisition

Resources

Encrypt Message