Meaningless Drivel is fun!*
The moose likes Threads and Synchronization and the fly likes Trying to speed up some calculations Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Trying to speed up some calculations" Watch "Trying to speed up some calculations" New topic
Author

Trying to speed up some calculations

Faisal Khan
Ranch Hand

Joined: Jun 29, 2003
Posts: 285
I have a loop for 1 upto 10 cars, inside the loop I call a SpecialOffersCalc class which sets the price, tax, mainteance cost etc for each car
and displays them in the JSP.
This for about 10 cars takes around 10 seconds to do the calculations. I thought in order to improve the speed of this page, I could have a loop and start a thread for each calc so that calcs can be done simultaneously.
Now the calculations in SpecialOffersCalc which is implementing Runnable are done inside public void run() {} - The page executes in a second but all resuls are null. I am useing an instance of SpecialOffersCalc and getter methods to retrieve the values.
Any help in the right direction will be most appreciated.
- FK


The secret to creativity is knowing how to hide your sources.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

If you're running the calculations in separate threads, then the main thread really needs to wait for those separate threads before continuing (if this weren't a JSP, there's be other options available, but in this case, you can't return a page until the calculations are done.) You're seeing "null" presumably because you're returning the page before the threads you've started have done their work.
This is a complicated subject, and I could show you an example or two, but you really should learn the fundamentals. I strongly recommend Doug Lea's book Concurrent Programming in Java.
Now, another thing you might consider is how to speed up the calculations. 1 second per car seems awfully slow -- are you hitting a database for each one?


[Jess in Action][AskingGoodQuestions]
Faisal Khan
Ranch Hand

Joined: Jun 29, 2003
Posts: 285
Thanks for the direction. I guess I will for now leave it, get my certification out of the way and then study this topic in more detail.
There are a lot of different values being pulled out of different tables, hence the speed.
I guess I'll just store the results in a database table as they will change infrequently.
- FK
Anupam Sinha
Ranch Hand

Joined: Apr 13, 2003
Posts: 1088
Hi Ernest Friedman-Hill
Originally posted by Ernest Friedman-Hill:

Now, another thing you might consider is how to speed up the calculations. 1 second per car seems awfully slow -- are you hitting a database for each one?

Can you suggest few ways through which we can speed up the calculations, without actually changing the formula. What if we are using a database to access the value. I am asking this question just out of curiosity.
[ July 25, 2003: Message edited by: Anupam Sinha ]
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

I can't suggest ways to speed up the calculations without knowing what they're like. You could show us code or explain more what you have to do for each car.
Anupam Sinha
Ranch Hand

Joined: Apr 13, 2003
Posts: 1088
Hi Ernest
Thanks for looking into my question. I am actually not the author of this thread and don't about the application. I wanted to know if there are any steps that can or may decrease the time for each computation or access time for database access.
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
The answer is still the same, I think. No - not without knowing more about the specific application.
Typically, in the Performance forum we discourage people from putting much effort into optimizing before they've tested their code to see how fast it is currently, and whether performance is really even a problem. In this case, we don't even have requirements, much less a coded solution to those requirements. I don't think there's much more that can be said here.


"I'm not back." - Bill Harding, Twister
Faisal Khan
Ranch Hand

Joined: Jun 29, 2003
Posts: 285
Just to say what I did to make the user experience better.
We have an admin screen where the admin guys sets the new vehicles for each corporate that we have. What I have done is rather than generating all the quotes real time (which does not really add any extra value), I have added a button in the admin screen where the admin guy after adding the vehicles can click to save the new quotes in the database.
The user page is now simply pulling out pre-calculated quotes from the database, which obviouslt is very quick.
I think I need to evaluate the underlying quoting mechanism of my application anyway. There are about 20 different queries at the moment that form part of a quotation, partly due to the way the tables were designed in the past.
 
jQuery in Action, 2nd edition
 
subject: Trying to speed up some calculations
 
Similar Threads
Running a quote in threads.
Question about using Multi-threading
Performance difference between different ways of iteration
Reading a CSV file--> Fastest way
session