0

if I have a 2 dimensional array (4*4) and I want to loop in the whole array elements

Which is faster? :

  1. use 1 thread and loop on the whole array by it.
  2. use 4 threads and loop the whole row by each thread.

which is better as a multi-core concept (performance - memory) !?

Thanks.

10
  • 4
    The array is too small to gain a performance by multiple threads. Commented Jun 25, 2013 at 20:07
  • @kan Not necessarily; it may, for example, take a really long time to process each element. Commented Jun 25, 2013 at 20:08
  • @arshajii Yes, but in this case the array is irrelevant Commented Jun 25, 2013 at 20:08
  • 1
    Depends highly on what needs to be done to each element in the array. If it is a processor bound operation then you can get some performance improvements if it takes a lot of time to process each one. Otherwise as @kan mentions, you probably won't get any improvement. Commented Jun 25, 2013 at 20:09
  • And, of course it also depends on how many cores you have at your disposal (in the 'each-element-requires-long-processing' case) Commented Jun 25, 2013 at 20:12

2 Answers 2

2

The actual looping will take next-to no time in either case. The detemining factor is what the threads are going to do with each element in the array - are the element simple POD types with trivial actions or are they objects with complex, lengthy, CPU-intensive methods?

It is self-defeating to create or signal multiple threads to perform such an action if the action performed on each element takes less than ~ 50ns, since this is how long it takes to signal the threads to start running your loops, and this is under optimal conditions where the four threads only requre to be signaled to start running the loops. Submitting the results to a pool would take longer. Actually creating new threads to run a 20ns task would be hopeless.

Sign up to request clarification or add additional context in comments.

Comments

1

Which is faster? Use 1 thread and loop on the whole array by it or use 4 threads and loop the whole row by each thread.

This depends highly on what needs to be done to each element in the array. If it is a processor bound operation that takes a while to run, then you can get some performance improvements by processing each element in its own thread (or by setting up a fixed thread thread-pool and submitting each element as its own task). Otherwise as @kan mentioned, you probably won't get any improvement.

I want just to print each element!

If the processing of each element is mostly an IO operation then you are going to be limited by IO and not CPU. In this case your program is not going to run any faster if you run each element's print operation in a separate thread.

which is better as a multi-core concept (performance - memory) !?

Performance is going to be the same. A single threaded solution will use less memory because only one thread is going to be used but the memory increase with multiple threads (at least with only 16) is relatively minimal.

5 Comments

Most likely it is not going to run any faster at all @MuhammedRamadanAdly.
+1 . Can't we sum together in this way: if four threads will be used to read each row then the overhead because of context switching of threads would make the entire operation to run in same (or more) time than if it is performed by single thread .. ?
Agreed @VishalK. If you need to then share the results between the threads, this can easily run slower than a single thread.
@VishalK - what context-switching? I'm assuming you have 4 cores? Have any of you actually done any benchmarking?
@MartinJames: Correct me if I am wrong..If we have 4 cores and 4 threads it does'nt mean that all 4 cores are free to execute the 4 threads in parallel. What if 1 thread is running on 1 core and rest three cores are busy in some other computations? In that case there won't be any context switching?? And regarding benchmarking..NO I have'nt done benchmarking.. Would be pleased if you let me know how to do this..or point to some link..thnx

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.