0

I'm making a server for a game and I need to save everyone's second so I made :-

private static void SubmitChanges() {
    while(true) {
        try {
            using(GameDBDataContext db = new GameDBDataContext()){
                foreach(IHero hero in world.Entities.BattleEntities.OnlineHeros.Values) {
                    hero.Update(db);
                }
            }
        } catch(Exception ex) {
            Console.WriteLine(ex.ToString());
        }
        Thread.Sleep(1000);
    }
}

Is that fine? and would it be fine if I have like 500 online heroes?

EDIT: Checking how long it takes :-

private void UpdateDatabase() {
    try {
        using(GameDBDataContext db = new GameDBDataContext()) {
            foreach(IHero hero in world.Entities.BattleEntities.OnlineHeros.Values) {
                DateTime now = DateTime.Now;
                hero.Update(db);
                DateTime after = DateTime.Now;
                Console.WriteLine((now - after).Milliseconds);
            }
        }
    } catch(Exception ex) {
        Console.WriteLine(ex.ToString());
    }
}

result :-

-13
-10
-13
-26
-19
-24
-25
-19
-27
-22
-19
-26
-25
-21
-24
-22
Picrofo Software
  • 5,475
  • 3
  • 23
  • 37
Abanoub
  • 3,623
  • 16
  • 66
  • 104
  • How long does it currently take to update 1 hero? Can't you just work it out from there? – MatBailie Nov 03 '12 at 19:03
  • I tested that it takes `-13 -10 -13 -26 -19 -24 -25 -19 -27 -22 -19 -26 -25 -21 -24 -22 ` millisecond per one update – Abanoub Nov 03 '12 at 19:22
  • As an aside, you may want to look into using Stopwatch to profile your code instead of DateTime. It'll give you more precise measurements. See: http://stackoverflow.com/questions/2923283/stopwatch-vs-using-system-datetime-now-for-timing-events – JoshVarty Nov 03 '12 at 19:25
  • How much info about hero do you want to save? And technically you don't open new dbcontext per hero. You do one update per hero. – Kirill Bestemyanov Nov 03 '12 at 19:41
  • 20ms each, 500 times, that's 10seconds. Is it okay to do that once a second? Ummm... Best find a way to update all 500 in one go, or reduce the overheads for each update until you're down to about 1ms each. – MatBailie Nov 03 '12 at 21:03

1 Answers1

0

You can update this all in one sql request. Linq-to-sql has method SubmitChanges, that commit all changes in context. So you can accumulate changes on hero in loop and submit them after loop.

private static void SubmitChanges() 
{
    while(true) {
        try {
            using(GameDBDataContext db = new GameDBDataContext()){
                foreach(IHero hero in world.Entities.BattleEntities.OnlineHeros.Values) {
                    hero.UpdateRecord(db);
                }
                db.SubmitChanges();
            }
        } catch(Exception ex) {
            Console.WriteLine(ex.ToString());
        }
        Thread.Sleep(1000);
    }
}

Where UpdateRecord is your Update method but exclude SubmitChanges.

Kirill Bestemyanov
  • 11,946
  • 2
  • 24
  • 38
  • technically this is not one sql request. Linq-2-sql will issue one sql statement per update. They will be wrapped in a transaction though. – Pleun Nov 04 '12 at 15:48