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

How to create RecordN for generated DAO lookup
Java 8: Map instead of simple loop
Detecting when an integer is made up of only the same digit
How to default an EditText to integer but allow decimal input?
Prime numbers no loop no recursive
JMX polling or TCP Pushing: What is better?
how can I stop the execution of the while loop with a keyboard input?
Profile neo4j database hits using java
Big O notation and not understading from class lecture
(javafx) I'm currently making an app which requires people to create an account, how can i make sure the texts fields aren't null?
Where file created in NetBeans
Can I force liquibase 3.5.1 to ignore legacy changeset checksum differences?
NoSuchBeanDefinitionException: No qualifying bean of type [duplicate]
how to add authorization in cxf
How not to lose focus?
JLayeredPane Not Appearing

Categories

HOME
maven
winforms
netbeans
cookies
path-finding
electron
framework7
kalman-filter
jsrender
android-youtube-api
pheatmap
kibana-4
facebook-php-sdk
gnupg
phaser
caml
after-effects
progressive-web-apps
solaris-10
lucene.net
visual-composer
scichart
grails3
physics-engine
tokenize
captiveportal
vlsi
kannel
filezilla
xacml
assistant
fabric8
bcd
phonegap
http-status-code-503
create-table
karaf
stringtemplate
protovis
windows-dev-center
openoffice.org
vxworks
hockeyapp
sqlite2
document.write
sql-server-agent
quadratic-programming
jvm-languages
android-mediaprojection
unixodbc
segment
websphere-mq-fte
auto-update
pnotify
aurelia-validation
jxcore
forever
pg-dump
r-forge
windows-iot-core-10
memory-alignment
mcafee
time-and-attendance
etsy
merge-conflict-resolution
python-stackless
nessus
oauth2client
wapiti
ford-fulkerson
coveralls
tarjans-algorithm
splash
sailfish-os
sniffer
pretty-print
apache-commons-net
rake-task
gadt
android-nested-fragment
rdl
castle
sgen
jquery-layout
mysql-error-1062
seaside
transcoding
pyhdf
xsockets.net
access-rights
dbconnection
cassini-dev
angularjs-controller
browser-detection
newtonscript
getmessage
postgresql-performance
f#-powerpack
rfc1123
winbugs14
floating
yslow
gallio
gin
modelstate
routedevent
kdbg
libs
ti-dsp
boost-smart-ptr
temporal-database
putchar
dbisam
document-conversion

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