.Net 4.0 Threading isn't any faster?

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?
LVL 13
RyanProject Engineer, ElectricalAsked:
Who is Participating?
 
käµfm³d 👽Commented:
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
 
CodeCruiserCommented:
Threads are probably waiting for access to list so that is causing the issues.
0
 
RyanProject Engineer, ElectricalAuthor Commented:
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
Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

 
CodeCruiserCommented:
There will be only one process I think. And not all code is CPU intensive. IO is usually the biggest hog.
0
 
käµfm³d 👽Commented:
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
 
RyanProject Engineer, ElectricalAuthor Commented:
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
 
käµfm³d 👽Commented:
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
 
RyanProject Engineer, ElectricalAuthor Commented:
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
 
RyanProject Engineer, ElectricalAuthor Commented:
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
 
käµfm³d 👽Commented:
Is DiskBuilderNonThread the method that contains the recursion? Does it effectively do the same thing as DiskBuilder, but it just calls itself?
0
 
RyanProject Engineer, ElectricalAuthor Commented:
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
 
RyanProject Engineer, ElectricalAuthor Commented:
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
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Well played kaufmed.  Well played.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.