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

Representing tree data structure in REST as URL sequence
How to refactor a large class with many methods that have single responsibility?
Spring scan all controller when it is not declared in component scan
About getContentLength method of HttpUrlConnection of java in a Android Program
Zabbix JMX monitoring host is not reachable
Java Move File and apply Attributes of Directory in Windows
Android Studio (Java) Replace function [closed]
java: dynamic class cast
Not able to load https url when using selenium webdriver + phantom
How to resolve this selection sort in ArrayList
Change color of JTabbedPane border
How can I “go to” a specific row in a JTable when user press a JButton?
msf4j oauth2-security is throwing null pointer exception
Android SurfaceView scaling error (ThreadedRenderer)
How to call a SSL secured REST service within AWS Lambda
Segregating rows on group count basis-TALEND

Categories

HOME
atom-editor
openmp
minimum-spanning-tree
mediawiki
homebrew
sd-card
webstorm
framework7
adb
razor
jxls
baqend
user-input
spring-xd
floating-action-button
numeral.js
etl
messages
windows-server-2012
ups
seaborn
highlight.js
tostring
windows-7-x64
invantive-sql
evopdf
normalizr
physics-engine
sylius
captiveportal
java-7
numerical-methods
underflow
fgetcsv
fluentvalidation
oracle-fusion-middleware
fog
kendo-ui-grid
windows-dev-center
asset-pipeline
host
restlet
hockeyapp
abstract-class
objectlistview
ssjs
sqlite2
broadcastreceiver
pdf-reactor
password-encryption
bootstrap-dialog
node-sass
prolog-setof
python-c-api
return-value
font-size
jspdf-autotable
heidisql
probability-density
log4c
underscore.js-templating
google-web-starter-kit
multiple-regression
clang-static-analyzer
bluemix-app-scan
ptrace
vhd
maven-tomcat-plugin
msys
teamcity-8.0
interrupted-exception
method-parameters
fluid-dynamics
census
notify
formatjs
tablelayout
java.util.concurrent
valueconverter
doskey
mysql-error-1062
android-2.2-froyo
fmod
gnu-smalltalk
multiprocessor
odata4j
html4
django-nonrel
jquery-mobile-dialog
eventlistener
chuck
funscript
first-responder
regsvr32
assembly-loading
work-stealing
locationlistener
web-application-design
gamma
dentrix
gin
nhibernate.search
iweb
privilege
defensive-programming
dbisam

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