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

SonarLint Eclipse - Analyze all? [duplicate]
list.clone is prefered over new ArrayList(originalList)
How to get functionality like placing a scene with in a scene in JavaFx 3d
How to set camera to the canter in preview mode? Andengine Live Wallpaper
unable to deploy EAR using jython in WAS 8.0 commadline
Java windows write bytes to file, some are incorrect [duplicate]
Spring Boot - Stomp : '.' vs '/' in Message Destination
Read input files on the fly in Spark
Java: How to dynamically reload data parser class and bot for companion robot?
Jsp Img tag Failed to load resource: the server responded with a status of 404 (Not Found)
Versions Maven Plugin: package not found after versions:use-latest-versions
package cucumber.api.java.en does not exist cucumber
How does mongo driver choose the node to connect?
Java - Display 1and2 3and4… loop with input
Cannot set Text for a button in onTimeSet
How to send Email with Java 6 SE?

Categories

HOME
compiler-construction
proxy
cookies
deezer
push-notification
layout
malloc
sharepoint-designer
gps
ebean
fingerprint
django-imagekit
iggrid
postgres-xl
commonmark
windows-phone-7
reactive-cocoa
http-status-code-504
ab-testing
viewport
zapier
kryo
bootstrap-tour
language-agnostic
trading
firebase-crash-reporting
fgetcsv
entitlements
javascriptcore
yadcf
create-table
lxd
xmlreader
android-kernel
mmenu
catalog
bytecode-manipulation
dartium
serverside-rendering
code-search-engine
trim
jvm-languages
ibpy
nand2tetris
password-encryption
no-www
python-c-api
typescript1.8
sage-one
tizen-native-app
integrity
nodebb
pillow
background-service
spring-android
gridpane
httplistener
log4c
py2app
gstreamer-0.10
ubuntu-10.04
master-slave
hexdump
tcpserver
google-places
wireshark-dissector
purge
oberon
web-controls
apache-commons-net
geonetwork
markers
php-5.4
valueconverter
dealloc
ember-charts
gnu-smalltalk
aqtime
random-seed
dsn
onsubmit
buildr
gnu-prolog
memory-size
javap
text-coloring
project-hosting
zune
windows-live-messenger

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