|
Optimizing code: Hunters |
Kikaru
Member #7,616
August 2006
|
Well, as I am developing my current game, Hunters, I have seen some freezing of the program. This is mostly due to only having half the memory I normally do, but it made me think: how can I optimize my code? I have included both the main file and the header with this post. I hope my code is well documented. Cookies for help! |
Jonatan Hedborg
Member #4,886
July 2004
|
My eyes are bleeding. No offense, but you really need to split up that code a bit more before i will even try to look at it seriously. Your main function is about 470 lines long. But enough of that At a glance; while (clock() < log_time) {} Use proper timing (search the forum), and see if the problems clear up.
|
Goalie Ca
Member #2,579
July 2002
|
Use a profiler and isolate some functions for us or write some pseudo code.. i don't have time to go through code but if you posted snippets or ideas i and others could make some quick comments. In terms of memory: pass by reference, mmap large files, use a memory pool if you have lots of allocation/deallocation. Make your contructors as simple as possible and use initialization syntax for variables rather than assignment. (some compilers still haven't figured this out in all cases). ------------- |
Jonatan Hedborg
Member #4,886
July 2004
|
I really don't think you have anything that is overly time-/memory- consuming. Try it yourself:
|
Kikaru
Member #7,616
August 2006
|
Well, I did this: long timer; void plus_time() {timer++}; install_int(plus_time, 1);
instead of clock(), and it runs a lot better now. Thanks! |
Jonatan Hedborg
Member #4,886
July 2004
|
You should try to think about the design before you start coding, so you don't have to write everything as a global "just in case". But anyway; glad it worked better with allegro's timer Also, you should read the timer section in the manual fort he "proper" way to do it (volatile etc).
|
Kikaru
Member #7,616
August 2006
|
I have considered replacing the shot and enemy arrays with vectors. Think that would help at all? |
SiegeLord
Member #7,827
October 2006
|
I think it would. It would conveniently remove the cap on max number of enemies... "For in much wisdom is much grief: and he that increases knowledge increases sorrow."-Ecclesiastes 1:18 |
Kikaru
Member #7,616
August 2006
|
Oh. Yeah. Cool. BTW, the actual max number is 80 right now... Hey, I think I found something: When I include <vector>, I get warning about includes with: |
Peter Wang
Member #23
April 2000
|
Quote: clock() has very (!) poor accuracy; it only changes value about 6-7 times per second (on winXP). It does not do what you think it does...
|
Kikaru
Member #7,616
August 2006
|
Well, anyone know why <vector> is trying to include "windows.h"? Please? |
Jonatan Hedborg
Member #4,886
July 2004
|
It returns the number of "ticks" (1000/second on my machine) since the process started. Only, it does so in rather large "jumps". Or am i wrong?
|
Goalie Ca
Member #2,579
July 2002
|
kikaru, can you paste all of your include statements in order. ------------- |
Kikaru
Member #7,616
August 2006
|
Ok, here: #include <allegro.h> #include <math.h> #include <stdio.h> #include "hunters.h" #include <vector> hunters.h has this in the include: #include <allegro.h> #include <math.h> #include <stdio.h> #include <vector>
[EDIT] [EDIT 2] |
|