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 add a border to a checkbox and make it always visible
Running .bat file through Task Scheduler doesn't work, however when running the .bat file manually it works
classcastexception: cant be cast to com.sun.jersey.spi.container.servlet.ServletContainer [duplicate]
Use of debbugger in eclipse to get method called in a specific moment
Does nested method calls with Spring AOP proxyTargetClass=true
Groovy with Java OpenCV bindings (JNI)
Unfortunately, *GridView* has stopped
Hibernate: Additional annotation to Column from inheritance
Access Database created from other class (Android/JAVA)
Creating a for loop to double the previous value
Hashmap hashtable linkedHashmap what to use?
Using `#ConfigurationProperties` annotation on `#Bean` Method
Java downcast objects invoking sub method [duplicate]
Deserializing json that is false or object with Retrofit GsonConverterFactory
Jackson unmarshal: store inner xml as string
Remove Method for a Single and Double Linked List (Java)

Categories

HOME
bluetooth
single-sign-on
angular-material
adb
jira
spring-cloud-stream
sql-server-2016
ravendb
android-youtube-api
virtualization
google-translate
communication
spring-kafka
opentracing
saxon
pc
interop
xlsxwriter
autoconf
physics-engine
abi
tar
user-interaction
fopen
kendo-datasource
objectanimator
c++-amp
google-cloud-nl
gsmcomm
gsoap
karaf
scorm2004
column-family
caret
accelerate-framework
hexo
overriding
appfabric
suricata
main
event-driven
dism
email-templates
texmaker
logfiles
dotcover
jedis
csound
composite-key
windows-mobile-6.5
mako
sqldf
jspdf-autotable
django-scheduler
azure-sdk
infix-notation
slicknav
ado.net-entity-data-model
dstu2-fhir
file-writing
transmitfile
google-web-starter-kit
bluemix-app-scan
ios8-today-widget
xna-4.0
marching-cubes
angular-leaflet-directive
riak-cs
interrupted-exception
nsmutabledictionary
fscommand
java.util.concurrent
mesa
monomac
tws
easy-install
cos
hungarian-algorithm
random-seed
clipper
factory-method
pvrtc
ticoredatasync
chrono
osql
removeclass
nsdatecomponents
tomcat-valve
f#-powerpack
xmemcached
coda-slider
asp.net-profiles

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