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

Extending java configuration in spring
Spring Web Flow - Handle concurrent access
Use default and custom liquibase configurations in spring boot application
creating new excel rows when the column reaches a specific number
JSch cumulatively causes Oracle JVM to grab all file handles and all memory on machine - Centos 7 - Java SE 1.8
Change cursor in JavaFx for long operations implemented for multiple background actions
Read external property files from JBoss server using Spring framework
org.springframework.orm.hibernate4.LocalSessionFactoryBuilder.addAnnotatedClass(Ljava/lang/Class;)
how to convert a java list to a scala List [duplicate]
Spring transactions - required new - rollback for parent
Java AI Not Following the Player
want to put 'play' icon for some of the listview items
Do Jpa& Hibernate load data which changes asynchronously in DB?
null checkbox selections from Thymeleaf
Sorting Strings that contain integers with comparator
Make code more Generic in Java

Categories

HOME
java
image-processing
mediawiki
mfc
nullpointerexception
relayjs
tesseract
relay
bpmn
webstorm
youtube-api-v3
azure-media-services
portia
node-notifier
adobe-analytics
offline
imacros
visual-studio-2005
serverless-framework
workload-scheduler
foselasticabundle
footer
oracle-coherence
cloudhub
facebook-instant-articles
extjs5
core-text
bootstrap-tour
jndi
claims-based-identity
su
bootstrap-duallistbox
applozic
google-cloud-nl
r-raster
bluestacks
http-status-code-503
airconsole
titanium-mobile
pdb
contact-form
winrt-xaml-toolkit
textmate
android-kernel
mapdb
revolution-slider
simplexml
git-merge
jquery-bootgrid
c11
eclipse-scout
estimote
businessworks
cubic-spline
wso2carbon
long-polling
flickr-api
pango
promela
datastax-startup
git-diff
mplayer
darcs
composite-key
nodebb
qtwebview
mako
jspdf-autotable
synchronous
cubes
measures
flash-cs5
ready-api
sdf
bgp
oauth2client
tform
eclipse-clp
angular-leaflet-directive
appfabric-cache
apache-commons-net
ng-animate
dd
eol
dealloc
oracle-warehouse-builder
ruby-datamapper
jplaton
ora-00911
qt-faststart
smtp-auth
typoscript2
dmoz
datawindow
qtkit
gwt-ext
stage
javax.script
appendto
krl
sudzc
xsdobjectgen
castle-monorail
anti-piracy
pascal-fc
port-number
data-acquisition
lzh

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