Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

the runtime is very slow when you have a lot of light functions #19

Closed
NoureddineHsaini opened this issue Mar 19, 2019 · 2 comments
Closed
Assignees

Comments

@NoureddineHsaini
Copy link

NoureddineHsaini commented Mar 19, 2019

first of all thank you for code sharing,
Can you help me please,
I tested your code in my projet, but i found the runtime is very slow, compared to the normal code (without threadpool) or compared normal thread. For exmeple my code like this:
//////////////////////////////////////////////////
void myfunction(int k){
std::vector myvetcor;
myvector.push_back (k*2.77+25/5);
}
int main()
{

 {
  ThreadPool pool{ 8};
   for (int i = 0; i < 20000; ++i)
		{
			pool.enqueue([ i]() {
                                                myfunction(i);
			});
		}
	}

return 0;
}
////////////////////////////////////////////////////
thank you

@mtrebi
Copy link
Owner

mtrebi commented Mar 20, 2019

Hey!

I'd say the function is too small (plus is allocating and deallocating the vector each time is called). In cases like this one, it may be counter productive to use the thread. This is because the overhead that the thread pool adds (to keep track of the queue, callbacks, etc) is too big in comparison with the time required to execute the function, which is really really small.

If you want to execute the same light function multiple times, you can probably build a less generic thread pool that is optimized for that. Nevertheless, for such simple math operations, the overhead may still be too high.

This article may be of your interest: https://en.wikipedia.org/wiki/Amdahl%27s_law

Hope this helps!

@mtrebi mtrebi self-assigned this Mar 20, 2019
@NoureddineHsaini
Copy link
Author

Thank you so much

@mtrebi mtrebi closed this as completed Jun 1, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants