Single Threaded vs Multi Threaded in terms of performance

  So do we have any advantage of being on any one side of threading ? let us see!!

  Single Threading:-

Simply it is the process of issuing one command at a time to the CPU.It is somewhat confusing term.

  Multi Threading:-

The ability of an operating system to execute different parts of a program  simultaneously. The programmer must carefully design

the program in such a way that all the threads can run at the same time without interfering with each other..,this is called Multi threading.

server side:-  Whenever multiple requests comes from the same user then each request is made as a thread (read complete article to understand this point)…anything that serves as concurrent request from clients (databases servers) are good examples of multi threading.

                           Calculating Performance:-

LaymanTerms:-

let us assume there are 8 tasks to be performed . here task can be any  part of a big program(like e.g:- functions or methods in object oriented world)

 In case of Single Threaded programs or process   (single thread or multi threaded depends and must supported by operating systems and proccessor architecture)

Complete task1 –>Complete task2 –>Complete task3 –>Complete task4 –>

Complete task8 <– Complete task7<– Complete task6 <– Complete task5 <–

so as seen from above one task must be completed to start other

In case of Multi Threaded programs

 Thread 1:

complete task 1–>complete task 2

  Thread 2:

complete task 3–>complete task 4

Thread 3:

complete task 5–>complete task 6

Thread 4:

complete task 7–>complete task 8

       As seen above each thread took 2 tasks and now each thread will execute on single core (that is if 4 cores are there then 4 threads can execute simultaneously!!)

At a first look on the above models one concludes multi threaded are faster..but wait a second!!

There is a term called context switching that must be considered when talking about multi threading.

The factors which must be considered are:-

mainly depends on how many CPU’s do you have

–>   do you have single core(processor) then if the process in queue are no I/O blocking then Single thread is more useful (because no context switching).

  –>   but if you have single core and process in queue are I/O blocking (i.e I/O process is being blocked frequently then the next I/O process must wait in the queue and hence during this time processor will be idle  because processor based process may  be waiting behind I/O process)   then multi threading (even in single core) is  better.

–> but if you have multiple CPU’s then blindfoldedly one should go with multi threading at any factor

    But is there any application which requires only single threaded specifically rather than Multi-threading ? 

(This question can be posed basing on a premise that Multi-threading is definitely faster than single threaded.)

the answer depends on two factors

1) if the application don’t require to exploit maximum CPU efficiency

2)If priority inversion is not a big deal (i.e no need of avoiding it )

( priority inversion means more priority process stay behind low priority process during execution i.e low (disk or network I/O’s blocks and prevents high priority ones(like user interface) in responding to the events)

In nutshell if there is not much to be dealt with user interface or people attached to it don’t bother latency and if it doesn’t require more than one core processing power then go for single threaded applications

e.g:–batch process

In case of web servers, the deal with lot of memory and hence page faults increases, as a result of page faults which are a form of disk I/O’s (i.e page fault implies stuff that is not present on main memory must be fetched from disk this is called page fault , that means whenever you need to bring from disk that increases time i.e disk I/O that is low priority ones are blocking high priority (User interface events ) hence priority inversion.

so in this case single threaded should not be used. as in the above lines I mentioned web servers creates a thread for each user request and the reason for that is to achieve multi-threading.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s