[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

.Net 4.0 Threading isn't any faster?

Posted on 2012-09-17
13
Medium Priority
?
347 Views
Last Modified: 2012-09-20
I have a Intel Core 2 Duo, processor, which to my understanding can run 2 processes at a time.  

I have a function which makes recursive calls and adds results to a List which is a 'global' variable.

I roughly timed a specific process to take about 18seconds without using threading. I then divided the work into 2 halves and called
Threading.Tasks.Parallel.For(0, 1,  Sub(x) 
...
End Sub)

Open in new window


It takes the same amount of time.
I watched my System Processes and I only see 1 instance of my code, and its only using about 15-20% CPU with 50-60% being idle.  No errors.

Is the List implementation causing it to not run in parallel rather than throwing an error? Should I be using a different methodology?
0
Comment
Question by:Ryan
  • 6
  • 4
  • 2
  • +1
13 Comments
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 38407122
Threads are probably waiting for access to list so that is causing the issues.
0
 
LVL 13

Author Comment

by:Ryan
ID: 38407287
I tried changing my List to Concurrent.ConcurrentBag, and it might have improved it from 18.5s to 18s (according the Stopwatch).

I still don't get why when this is running my CPU is 60%+ idle, and the process is <10%.  Still only seeing 1 process, too.
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 38407344
There will be only one process I think. And not all code is CPU intensive. IO is usually the biggest hog.
0
 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 38408037
I have a function which makes recursive calls and adds results to a List which is a 'global' variable.
That is not thread-safe, so I hope you employed some sort of locking mechanism!

You need to realize also that sometimes threading can result in less performance. It can be the case that the overhead of creating threads can be more expensive than simply running the non-threaded logic. Also, having too many threads can bog down a system, and end up being less efficient than fewer threads.

Can you provide an example of what you were attempting to do with threading?
0
 
LVL 13

Author Comment

by:Ryan
ID: 38408042
I did in the original?  I'm making 2 threads. The function combines some iteration and recursion, but there's no IO going on.  

I could change it such that each thread returns a list, but I figured passing that extra pointer was more overhead.

I thought a global ConcurrentBag would be threadsafe?  All I'm doing is adding to the List/Bag.
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 38408057
I did in the original?  I'm making 2 threads. The function combines some iteration and recursion, but there's no IO going on.
I was asking for the "..." of your example  = )

I thought a global ConcurrentBag would be threadsafe?
Yes, ConcurrentBag is thread-safe; List is not.
0
 
LVL 13

Author Comment

by:Ryan
ID: 38409364
Threading.Tasks.Parallel.For(0, 1,
				 Sub(x)
					 DiskBuilder(New List(Of TempDisk), _
					  If(x = 0, 0, CInt(Math.Floor((lstDisk.Count - 1) / 2))), _
					  If(x = 0, CInt(Math.Floor((lstDisk.Count - 1) / 2) + 1), lstDisk.Count - 1), _
					  lstDisk, V1mAMin, V10kAMax, HeightMin, HeightMax)
				 End Sub)

Open in new window


			Private Sub DiskBuilder(ByVal CurrDisks As List(Of TempDisk), ByVal StartIndex As Integer, ByVal StopIndex As Integer, ByVal lstDisks As List(Of TempDisk), ByVal V1mAMin As Double, ByVal V10kALeft As Double, ByVal HeightMin As Double, ByVal HeightLeft As Double)

				For i As Integer = StartIndex To StopIndex
					'Check if this disk won't exceed V10kA
					If lstDisks(i).V10kA <= V10kALeft Then
						'Add this disk to the build
						CurrDisks.Add(lstDisks(i))

						'Check if V1mAMin and Ht are met
						If CurrDisks.Sum(Function(x) x.V1mA) >= V1mAMin AndAlso CurrDisks.Sum(Function(x) x.Height) >= HeightMin Then
							'This is a good build, save it
							Builds.Add(New Build(CurrDisks, _Parent))
						ElseIf HeightLeft >= lstDisks(i).Height Then
							'Theres enough room in the housing for this disk
							'Not met, go deeper
							DiskBuilderNonThread(CurrDisks, i, lstDisks, V1mAMin, V10kALeft - lstDisks(i).V10kA, HeightMin, HeightLeft - lstDisks(i).Height)
						End If

						'Remove disk that was just added
						CurrDisks.RemoveAt(CurrDisks.Count - 1)
						'Else
						'	'Went over the max, sorted asc, so everything else will go over - this thread is done
						'	Exit For
					End If
				Next
			End Sub

Open in new window

0
 
LVL 13

Author Comment

by:Ryan
ID: 38409394
I replaced every reference to a List with a concurrentBag, and if anything it slowed it down from 18 to 19s.

Is there a VS setting I have to set to allow threading?
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 38412368
Is DiskBuilderNonThread the method that contains the recursion? Does it effectively do the same thing as DiskBuilder, but it just calls itself?
0
 
LVL 13

Author Comment

by:Ryan
ID: 38413626
They're essentially the same. I was able to remove 1 parameter for each subsequent call after the first (it was needed to divide the work in half for the 2 threads).  I thought it might be quicker with less parameters (StopIndex is removed).
0
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 2000 total points
ID: 38415973
I just noticed something:  I believe you are only queuing up one thread! The first two parameters to Parallel.For are fromInclusive and toExclusive (note the "exclusive" on the latter). You specified 0 and 1, respectively. That means you only specified one partition. Try bumping the second parameter up to 2. That should give you two threads.
0
 
LVL 13

Author Comment

by:Ryan
ID: 38418362
The exclusive parameter was the key. My results are now out of order, so I know there's 2 threads working.  It didn't speed up the process at all, but I got the answer to the question anyhow.  Thanks!
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 38418380
Well played kaufmed.  Well played.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
Screencast - Getting to Know the Pipeline
Suggested Courses
Course of the Month18 days, 8 hours left to enroll

825 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question