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

Adding existing Tomcat deployment to Netbeans
calling url or webservice in spring batch
StackOverlflowError with HQL query containing huge number of OR conditions
Accessing data from Cassandra using java
Issues when setting a text in a text view in Android Studio
Can't make plain web service work in plain servlet [Netbeans, glassfish]
JAX-RS PathParam not working
EDIT - Java while loop means events aren't called
android:fitsSystemWindows=“true” only works for one view
Winrun4j: Exceeded maximum classpath size
Duplication Criteria in Sonar
How can we read default cell format from Excel in Apache POI?
Need LWJGL Setup Guide For Eclipse
How can i calculate many week days in a month?
Get distances in miles for 2 longitudes or 2 latitudes only
Java code to PHP (base64, bytes)

Categories

HOME
pandas
bluetooth
reflection
mediawiki
fluentd
angular-material
spagobi
amortized-analysis
electronics
v8
mouse
metatrader4
here-api
nstableview
hex-editors
finite-automata
apache-metamodel
connection-string
samoa
jprofiler
plunker
custom-wordpress-pages
visjs
accessor
crystal-reports-2010
status
maquette
saas
subdomains
preg-match
stormpath
binary-data
ecto
siesta-swift
gammu
nssegmentedcontrol
overriding
x11-forwarding
serverside-rendering
service-discovery
unspecified
ssjs
ansible-playbook
opshub
scorm
angular-resource
revapi
worksheet
tropo
jxcore
csound
worker-thread
directory-structure
flutterwave
crosswalk-runtime
pintos
angular-strap
parallel-data-warehouse
jquery-filter
deadbolt-2
passport-google-oauth
py2app
nsfilemanager
storekit
essence
whois
two-factor-authentication
lib.web.mvc
t4mvc
simple-framework
drawbitmap
service-accounts
web-controls
sonarqube5.1.2
umbraco6
cctv
typeof
jubula
mechanize-ruby
balanced-payments
message-driven-bean
imdbpy
quantlib-swig
gridfs
bulkloader
cisco-jtapi
propertyeditor
jspinclude
cufon
adk
window-management
deobfuscation
paster
suppress
routedevent
perfect-hash
nintendo-ds
ctp4
scripting-languages
wsdl.exe
.net-1.0

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