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

Table column order generated by Hibernate
Converting Matrix to Graph using Jung
How to validate request parameter key in springs
Why is the the class not found error still showing up for Microsoft sql
How to calculate Big O for this algorithm?
Spark SQL: Nested classes to parquet error
Not able to get element by Xpath in Selenium webdriver
Draw data from InputStream every fixed byte length?
== and equals behaviour in java when string is concatenated at run time [duplicate]
Accessing URL with params in Spring Security
Calculating Tarloy series in Java
Learning Java GUIs -
Trying to Manipulate String in Java and keep getting “”boolean value" [closed]
Android chat client: socket connection
ArrayOutOfBoundsException in nested for loop
Android: can I put Global Variables in BroadCastReceiver?

Categories

HOME
variables
proxy
google-api-php-client
relayjs
vmware
sd-card
rsyslog
filtering
maven-3
jgroups
installshield
ezpublish
azure-media-services
cross-validation
resize
quickfix
serverless-framework
google-static-maps
django-cms
swingx
ml
tibco-mdm
icloud-api
opennlp
traffic
fluentvalidation
windows-server-2000
preg-match
mozilla
mpmediaquery
catalog
ios5
jquery-bootgrid
scaffold
swift3.0.2
vapor
impersonation
grid.mvc
nand2tetris
nomethoderror
isbn
acoustics
menuitem
dotcover
xml-documentation
osx-mavericks
appcompat
strptime
sqlclient
libusb-win32
memory-alignment
linode
spring-cache
dynamics-sl
account-kit
bstr
lttng
pagedlist
moveit
actionbardrawertoggle
nessus
browser-link
javax.sound.midi
pervasive-sql
jmeter-maven-plugin
jqgrid-formatter
dukescript
muse
php-ci
fluid-dynamics
system32
dia
oxwall
xc16
issuu
uitouch
p4java
ember-charts
xsockets.net
frameset
mod-auth
coredump
hamachi
html-input
wise
mozilla-prism
forums
sef
iweb
zune
lzh





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