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

Java Graphics Not Executing Paint Function Without extends
Function chaining in Java
How to match the key and value of the map return type?
How to send email using user's email ID from within the app (Without Intent)
How to print an object several times in Java?
Java Spring set custom entity resolver for web service xsd parser
JavaMail silently fails
Can't change .properties file path on eclispe
Create a box using nested loops and JOptionPane
Java - Creating an object within a method call [duplicate]
How to use Deelang conditionals with operators?
findviewbyid - Cannot Resolve
How can I include css file using com.sun.net.httpserver?
What is the proper way to stop hiveserver2?
What is the difference between ofInt, ofFloat, and ofObject for ObjectAnimator?
I don't even know how to get access permission of nested interface in Eclipse. [Scope issue]

Categories

HOME
ionic-framework
date
pycharm
angular-material
stock
session
platform-builder
kalman-filter
bookshelf.js
google-oauth
nano-server
midi
izpack
etl
phaser
opentracing
sms-gateway
text-rendering
beyondcompare
autocad-plugin
riot.js
clickonce
scriptcs
csrf-protection
bootstrap-tour
dxf
buck
preg-match
frame
iframe-resizer
usbserial
bytecode-manipulation
android-ble
ios5
io-redirection
suricata
dartium
retina-display
document.write
temporary-files
squib
ncalc
vao
azure-application-gateway
apache-fop
atomicity
mu
tropo
google-perftools
imanage
httplistener
merge-conflict-resolution
bstr
sdf
php-parse-error
two-factor-authentication
prerequisites
msys
kcachegrind
embedded-code
graph-api-explorer
truevault
bigbluebutton
java.util.concurrent
tld
contenttype
comexception
hyprlinkr
ember-charts
message-driven-bean
centos5
mysqltuner
inbox
bulkloader
shellexecute
jquery-mobile-dialog
xamlparseexception
html-editor
type-equivalence
robotics-studio
bitsharp
cisco-jtapi
hirefire
site.master
.nettiers
revisions
sustainable-pace
genealogy
w3wp
data-acquisition

Resources

Encrypt Message



code
soft
python
ios
c
html
jquery
cloud
mobile