• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

code speed up

 
Nora mono
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear all,

i have java code written sequentially and i need to make it parallel code
i used threads to accomplish this
but it requires more than the seq. one
then i used only two threads and divide the work between them also it takes longer time
finally i used one thread it become seq. but it still need more time !!!
after that i tried a very simple code ( just it print message) also the seq. time is much better !!!

can any one explain that to me
i need to speed up the code not makes it much longer

thank you
 
Henry Wong
author
Marshal
Pie
Posts: 20882
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
noony mm wrote:Dear all,

i have java code written sequentially and i need to make it parallel code
i used threads to accomplish this
but it requires more than the seq. one
then i used only two threads and divide the work between them also it takes longer time
finally i used one thread it become seq. but it still need more time !!!
after that i tried a very simple code ( just it print message) also the seq. time is much better !!!

can any one explain that to me
i need to speed up the code not makes it much longer



It sounds like you are under the impression that threads make "code speed up". It doesn't do that.

All you are doing, by splitting a single task, into separate tasks via threads, is to enable more resources. The idea is, if there are CPU cores previously not being used, they can now be used to run these extra threads.

However.... There is extra work to split/coordinate/merge these threads. There is extra work to synchronize these threads. And if your computer don't have extra CPU cores to spare -- all you are doing is this extra work so that your threads can share the same CPU core. And if you do a poor job at splitting the task into smaller tasks, or in a way that they can't run parallel with little synchronization efficiently, you can easily make the program take longer to complete (even with extra CPU cores helping out).

Henry
 
Edward Harned
Ranch Hand
Posts: 291
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Splitting the work into segments and processing those segments in multiple threads is what Fork/Join is all about. As Henry said, if you don't have the cores to do the work then stop right here.

If each piece of work is unique, that is a component in a request then you can look at the TymeacSE open source product here:
http://www.coopsoft.com/ar/ForkJoinArticle.html

If you need to dynamically decompose the work into identical tasks, with each task running in a separate thread then you can look at the TymeacDSE open source product here:
http://www.coopsoft.com/ar/ConquerArticle.html

Ed
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic