Threaded vs Non Threaded

BlitzMax Forums/BlitzMax Programming/Threaded vs Non Threaded

JBR(Posted 2009) [#1]
Hi, when I run the code below I get threaded=980 and unthreaded=1350

Why would 1 thread be quicker?


Strict
	

Function do_some_loops:Object( i:Object )

	Local j:Int
	Local superCalcResult:Double

	For j=1 To (String(i)).toint:Int()
	
		superCalcResult = j*Cos(j)+Tan(j)*j/j
	Next
	
End Function



Delay(2000)



Local time:Int = MilliSecs()

Local Thread1:Int = CreateThread(do_some_loops, "10000000")

WaitThread:Object( Thread1 )

time=MilliSecs()-time

Print "Thread :"+time



time:Int = MilliSecs()

do_some_loops:Object("10000000")

time=MilliSecs()-time

Print "Unthread :"+time



WaitKey()
End




ziggy(Posted 2009) [#2]
You're comparing a single thread operations to a single thread operation being done while another thread waits.

It is slower to use two threads than one when both the two threads are doing stuff at the same time, and the stuff made by one thread is nonsence. Waitthread IS do stuff,as the calling thread is performing a sync operation with the WAITED thread.
A proper design here would be to make half the calculations on the secondary thread WHILE the main thread is doing the other half.
After this, the main thread could wait for sync, and return the time used to make the operations REALLY using two threads.


JBR(Posted 2009) [#3]
Sorry, I don't follow you.

I am timing the time it takes for the function to be a thread against the time of just calling the function.

Jim

P.S. I am on an i7 processor if that makes a difference.


Gabriel(Posted 2009) [#4]
Why would 1 thread be quicker?

Quicker than what? Both times are for 1 thread. The only difference is that your first example sets itself up some overhead by pretending it's going to use multiple threads and then doesn't do it.


ziggy(Posted 2009) [#5]
No, you're comparing how much it takes to call a function and how it takes to create a thread to call this function while another thread checks if the thread function has ended.

If you want to compare performance of, lets say, 100 calculations using one thread or two threads, you sould implement it this way:

Single thread:
1.- Time mark
2.- Perform 100 iterations
3.- Get result

Duble threaded:
1.- Time mark
2.- Start a secondary thread to do 50 calculations
3.- MEANWHILE main thread performs 50 calculations MORE
4.- wait for the secondary thread to end (when the main thread has already ended)
5.- return results.

You should take into consideration the words MEANWHILE and MORE. That's where the speed diference may come :D I hope it is clear, didn't know how to make a step by step schema for this.


JBR(Posted 2009) [#6]
ziggy, I understand your Single & Double threaded example, but,

I still don't understand why just using the function 'do_some_loops' is slower than 'wrapping' it into a thread and waiting for the thread to finish.

Jim

It's probably just me! I think we should have a Threads forum:-)


Brucey(Posted 2009) [#7]
It's possible that the main thread carries some overhead...

But as ziggy points out, it's not a very useful example.
I also wouldn't have the main thread working on the same function (at the same time, as ziggy's double threaded example shows)... you'd be as well kicking off two threads to do half each, and let the main thread do whatever it needs to do elsewhere. Which is the whole point of threading... no?


JBR(Posted 2009) [#8]
Brucey, yes I was going to divide a bit of code into 2 threads as you say, but I just thought I would see what the difference would be by doing what I did. I was surprises that the createthread way was quicker.

Jim

Has anyone tried the code?


ziggy(Posted 2009) [#9]
JBR: I can't test the example now, but it would be a good idea to test it using the manual GC mode and compare it again, to see if it has any diference, as the object collection could be performed in the samne thread the function is running on the single thread example, making it slower than the separated thread version (taht's just an idea).
I also wouldn't have the main thread working on the same function (at the same time, as ziggy's double threaded example shows)... you'd be as well kicking off two threads to do half each, and let the main thread do whatever it needs to do elsewhere. Which is the whole point of threading... no?
Obviously, suposing you're doing something more than just comparing performance! :D