views:

65

answers:

3

Hi guys,

I have a very hard problem:

I have round about 20-50 objects, which I MUST (that is given for the problem, please don't spend time in thinking around it) put througt a logic EVERY SECOND.

The logic itself need round about 200-600 milliseconds (90% it is 200ms - 10% it is 600ms).

I try to find any solution how I can make is smaller, but there isn't. I must get an object from DB, I must have a lot of if-else and I must actual it. - Even if I reduce it to 50ms or smaller, to veriable rate of the object up to 50 will break my neck with the 1 second timer, because 50 x 50mx =2,5 second. So a tick needs longer then the tickrate should be.

So, my only, not very smart I think, idea is to open for every object an own thread and lead a mainthread for handling. So the mainthread opens x other thread. So only this opening must take unter 1 second. After it logic is used, the thread can kill itself and we all are happy, aren't we?


By given the last answers, I will explain my problem:

I try to build an auctioneer site. So I have up to 50 auctions running at the same moment - nothing special. So I need to: every single second look to the auctionlist, see if the time is 00:00:01 and if it is, bid automaticly (it's a feature, that user can create).

So: get 50 objects in a list, iterate through, check if a automatic bid is need, do it.

+3  A: 

With 50 objects and the processing time you've given on average you are doing 12 seconds worth of processing every second. Assuming you have 4 cores, you can get this down to an execution time of 4 seconds via threading. Every second. This means that you're going to start off behind and slip further behind as time goes on.

I know you said you tried to think of a way to make it more efficient, but couldn't, but I fear you're going to have to. The problem as stated now is computationally intractable. You're either going to have to process the objects in a rotating window (so each object gets hit once every 4th cycle or so), or you need to make your processing run faster.

Donnie
This is a good point - figure out which resources you're not utilizing, and if you and utilize them to help your problem. If you can't, then you're essentially out of luck. Threads, etc. won't magically fix anything.
Thanatos
Threading may help with throughput on a multi-core or multi-processor machine, but if it's a single core then you may actually slow it down with more threads.
Matt H
See my edits, maybe there is another solution
Kovu
+2  A: 

First: Profile, if you haven't already. Figure out what section of your code are taking time, etc. I'd go after that database - how long is the I/O of the objects from the database taking? Can you cache that I/O? (If you're manipulating the same 50 objects, don't load them every second.)

Let's address your threads idea: If you want multiple threads, don't create and destroy them every second. Create your X threads, and leave them be -- creating & destroying them are going to be expensive operations. You might find that less threads will work better - such as 1 or 2 per core, as you might be able to reduce time doing context switches.

Thanatos
+1 on profile first, +1 on don't hit the DB each iteration, +1 on don't create/destroy threads on each object, +1 on not too many threads. .... now, where's the +4 button? bah, it wasn't so interesting problem to start, lets round it to +1
Javier
+1  A: 

To expand on Jonathan Leffler's comment on the question, as the OP requested: (This answer is a wiki)

Say you have these three things being auctioned, ending at the times indicated:

10 Apples      - ends at 1:05:00 PM
20 Blueberries - ends at 2:00:00 PM
15 Pears       - ends at 3:50:00 PM

If the current time is 1:00:00 PM, then sleep for 4 minutes, 58 seconds (since the closest item ends in 5 minutes). We use the 2 seconds then for processing - adjust that threshold as needed. Once we're done with the apples, we'll sleep for (2 PM - now() - 2s), for the blueberries.

Note that when we wake up at 1:04:58 PM to process the apples auction, we do not touch the blueberries or the pears -- we know that they're still way out in the future, so we don't care.

Thanatos
Okay, I think with this less time it's realizeable. But in the reallity by me not. I have countdowns of 15 seconds, 10-20 of those, because if a bidder take a bid, the timer gets + 15 sec. So in the "hot phase" its very high
Kovu
If you have enough time to do the needed I/O, then check. Then you might find out you have another 15 seconds to wait. If you don't have enough time left to check, then there's nothing you can do - is there?
Thanatos