0

Creating threads significantly improves the performance of a program, but there is an upper threshold, beyond which if we create any more threads then the performance degrades, due to context switching.

Is there any way or formula, using which if all the hard-ware details required are know then one can find out the number of thread which would give the best performance??

It can be done experimentally, but I am looking at something faster, if some way one can have a educated guess, that's all.

Thanks,

Hemal
  • 265
  • 1
  • 7
  • 17
  • The amount of available processors is definitely one. You can get it through Runtime class. – Kayaman Feb 10 '14 at 14:14
  • Experiment. Depends whether you're IO bound or CPU bound. Start with 1 thread per core. – Bohemian Feb 10 '14 at 14:15
  • 2
    If all the threads will be doing nothing but computing (no I/O), then then a rule of thumb is available processors + 2. – Marko Topolnik Feb 10 '14 at 14:15
  • @Kayaman :Thank you for your reply, Yes that can be done, but I am more interested in optimized number of threads, which will give me the best performance... – Hemal Feb 10 '14 at 14:16
  • either you want the optimal number of threads, which you find out by testing, or you want a quick estimate on the number. You can't have both. – Marko Topolnik Feb 10 '14 at 14:18
  • @Bohemian : Yes experiment is certainly the best way, but if one want's to make a educated guess, then how can it be done?? – Hemal Feb 10 '14 at 14:19
  • @Marko Topolnik: I don't need both, I need a quick estimate, which if not very precise, should still be close... – Hemal Feb 10 '14 at 14:21
  • @MarkoTopolnik I've always wondered why n+2 (or n+1 as proposed in JCiP). – assylias Feb 10 '14 at 14:21
  • possible duplicate of [Threads configuration based on no. of CPU-cores](http://stackoverflow.com/questions/13834692/threads-configuration-based-on-no-of-cpu-cores) – assylias Feb 10 '14 at 14:22
  • @MarkoTopolnik: Yes, your first answer is one of the thing I was looking at, thank you, But if some I/O operations are also involved then? – Hemal Feb 10 '14 at 14:23
  • 1
    @assylias Rich Hickey used n+2 for Clojure's internal thread pool :) I guess it's as empyrical as everything else, but a reason why the extra thread may improve performance are delays due to locking. – Marko Topolnik Feb 10 '14 at 14:24
  • 3
    @Kabir Then it's time to start testing. If you use blocking I/O, the optimum numbers may get much, much larger because the threads could be spending >90% of the time waiting for data. – Marko Topolnik Feb 10 '14 at 14:25
  • @MarkoTopolnik : Yes, that's true, thank you for your reply. I knew it, but still though if given that only one line of your code per thousand line does I/O rest all is CPU intensive, then could we somehow make a call.. – Hemal Feb 10 '14 at 14:31
  • 1
    @Kabir if that one line is keyboard input, and you've just gone to make a cup of coffee, the other 999 lines are insignificant. Similarly if you make a disk request and the disk has to spin up or you make a network request over a satellite link. Your question is too broad. – Martin James Feb 10 '14 at 14:38
  • you can minimize no of testing tries with estimation but cannot reduce it to zero, it all depends on hw,your application behaviour,code,JVM etc. so try it once with educated guess and then do estimation that will give you better idea – Nachiket Kate Feb 11 '14 at 05:52

0 Answers0