Robert said:
Yep, threads on modern systems are very cheap. I once cooked up a small
program (attached) to collect thread stats.
I ran you program on my machine in both windows and linux and discovered
some interesting results:
The machine is a dual boot Thinkpad T60 with intel dual core. no special
systems/kernel optimisations has been performed on either systems.
linux: vanilla linux 2.6.17.8 kernel release running on Mandriva 2006
windows: factory installed windows xp with SP 2 (version 2002)
tf - linux:
max t11 - start time in thread: 55
avg t11 - start time in thread: 0.16632
max t2 - creation time : 42
avg t2 - creation time : 0.02114
max t3 - start time in main : 42
avg t3 - start time in main : 0.09306
max t11 - start time in thread: 65
avg t11 - start time in thread: 0.15874
max t2 - creation time : 15
avg t2 - creation time : 0.01887
max t3 - start time in main : 15
avg t3 - start time in main : 0.09395
tf - windows:
max t11 - start time in thread: 78
avg t11 - start time in thread: 0.66997
max t2 - creation time : 78
avg t2 - creation time : 0.14944
max t3 - start time in main : 63
avg t3 - start time in main : 0.27753
max t11 - start time in thread: 47
avg t11 - start time in thread: 0.73756
max t2 - creation time : 47
avg t2 - creation time : 0.14407
max t3 - start time in main : 47
avg t3 - start time in main : 0.29903
Conclusion: linux is faster.
I also tested a thread efficiency program I made, its a udp server and
client.
server: -t 1000 (number of threads: 1000)
client: -t 500 -r 10000 (number of threads 500,
number of requests per thread: 10000)
tf - linux
Average creation time for client object: 0.00462ms
Time executing threads: 183114ms (183.114s)
Average creation time for client object: 0.00426ms
Time executing threads: 182486ms (182.486s)
tf - windows
Average creation time for client object: 0.00359ms
Time executing threads: 535891ms (535.891s)
Average creation time for client object: 0.00296ms
Time executing threads: 536219ms (536.219s)
conclusion: windows is faster at creating client objects by a little
bit, but linux is 3 times faster at executing the actual operations.
I did another test with this code also:
in the server there is a sleep() call to simulate db access, I
experimented a bit with what values it could hold and how it would
affect the total performance. I found out that the performance
increasement is proportional to the sleep time decreasement, and that
all values down to 1ms (since it is the lowest value for the call I
made) affected performance. But for windows the story was completely
different, why that is I dont know. In windows any values below
100-110ms was rounded up to approx 100ms. So I could not get any
performance increase with values below 100ms. Also there was a strange
spike at the 1ms and 2ms tests (it might have something to do with
kernel context switching thresholds)
Here are the measurements:
tf - windows:
1ms:
E:\threads_perf>java -cp . tf.StatelessUdpClient -t 500 -r 10000
Time executing threads: 453875ms (453.875s)
Time executing threads: 483859ms (483.859s)
2ms:
Time executing threads: 656609ms (656.609s)
Time executing threads: 684734ms (684.734s)
4ms:
Time executing threads: 572547ms (572.547s)
Time executing threads: 604500ms (587.5s)
30ms:
Time executing threads: 578796ms (578.796s)
Time executing threads: 555860ms (555.86s)
100ms:
Time executing threads: 571079ms (571.079s)
Time executing threads: 593531ms (593.531s)
120ms:
Time executing threads: 632657ms (632.657s)
Time executing threads: 639125ms (639.125s)
150ms:
Time executing threads: 773750ms (773.75s)
Time executing threads: 771406ms (771.406s)
200ms:
Time executing threads: 1019234ms (1019.234s)
Time executing threads: 1021328ms (1021.328s)
500ms:
Time executing threads: 2543078ms (2543.078s)
Time executing threads: 2544656ms (2544.656s)
The code is attached.
tom