1

Let say there is an array of strings, I have to process. I'm using a «heavy» third-part object which gets the string and performs its analysis. In order to optimize a performance, I create a number of threads (according to a number of CPU cores), separate the array to the number of sub-arrays and each thread executes the logic described above with his portion of strings.

I have two options:

  1. To run the threads and inside of each thread initialize this «heavy» third-part object and pass strings. This means I initialize «heavy» object according to a number of threads.
  2. To initialize one time this «heavy» object and pass the reference to this object to each thread.

The approach #2 seems to me to be more correct, thus, it saves extra «heavy» object initialization.

My question:
Are there any pitfalls or design lacks with the implementation of approach #2?

1
  • 1
    #2 only works if the same HeavyObject can be shared between multiple threads processing the String. Unlikely, but check the documentation.
    – user949300
    Mar 21, 2016 at 20:37

1 Answer 1

2

There is no correct or incorrect way without knowing what problem you're solving and how you're solving it. There are several factors at play.

Some problems have expensive initialization that can be parallelized to perform better. Some perform the same or worse when parallelized.

Assuming parallelization doesn't help with initialization, there's a second issue. Sometimes sharing the same large object between threads slows things down because they have to compete for access to it.

Assuming parallelization does help with initialization, sometimes having multiple copies causes processor cache shared between cores to overflow, slowing things down compared to one copy even if you take the extra initialization time and competition for access into account.

I'm sure there are other factors as well.

Unless you're skilled at figuring all this really complex stuff out, I'd recommend implementing it in three ways (one shared object, one initialization that's broken into copies, and multiple initializations) and profiling each with examples that fit real world usage. Figure out which strategy performs better empirically.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.