performence boost for your project

Posted By: Zapan@work

performence boost for your project - 06/21/10 12:49

You can improve the performence of your project when not using the engine intern sheduler. Instead just use linked lists. We use this for our own projects (zombies etc.) wink

without linked-list:


with linked-list:


//////////////////////////////////////////////////////////////////////
// Link List Example
//--------------------------------------------------------------------
// Modul : Entity helper funcs
// Created : crew51 GbR : 2010-06-21
// $Id$
//--------------------------------------------------------------------
// todo:
/*

*/
//--------------------------------------------------------------------

#define USE_LINK_LIST

#include <acknex.h>
#include <default.c>

// Header
ENTITY* pEmeter_first;
ENTITY* pEmeter_last;

void ent_listFuncFrm(ENTITY* p);

#define ent_prevListObj skill96
#define ent_nextListObj skill97

//--------------------------------------------------------------------
// Add an object to the list
//--------------------------------------------------------------------
void ent_listAdd(ENTITY* p, ENTITY** first, ENTITY** last)
{
if (*first == NULL) {
*first = p;
*last = p;
return;
}

(**last).ent_nextListObj = p;
p.ent_prevListObj = *last;
*last = p;
}

//--------------------------------------------------------------------
// Remove from the entity list
//--------------------------------------------------------------------
void ent_listRemove(ENTITY* p, ENTITY** first, ENTITY** last)
{
//we're the last one
if (p.ent_nextListObj == NULL) {
if (p.ent_prevListObj != NULL) {
*last = p.ent_prevListObj;
(**last).ent_nextListObj = NULL;
} else {
*last = NULL;
}
}

//we're the first one
if (p.ent_prevListObj == NULL) {

if (p.ent_nextListObj != NULL) {
*first = p.ent_nextListObj;
(**first).ent_prevListObj = NULL;
} else {
*first = NULL;
}
}

//in the middle
if ((p.ent_nextListObj != NULL) && (p.ent_prevListObj != NULL)) {
ENTITY* p1 = p.ent_prevListObj;
ENTITY* p2 = p.ent_nextListObj;

p1.ent_nextListObj = p2;
p2.ent_prevListObj = p1;
}

}

//--------------------------------------------------------------------
// Test func for cube
//--------------------------------------------------------------------
void cube_rotate(ENTITY* p)
{
p.pan += 23 * time_step;
}

//--------------------------------------------------------------------
// Run all funcs
//--------------------------------------------------------------------
void ent_listExec(ENTITY* first, void* f)
{
ent_listFuncFrm = f;

ENTITY* p = first;
while (p != NULL) {
my = p;
you = NULL;

ent_listFuncFrm(p);
p = p.ent_nextListObj;

my = NULL;
you = NULL;
}
}

void cube_func()
{
while(1) {
wait(1);
my.pan += 23 * time_step;
}

}
void main()
{
warn_level = 99;
level_load(NULL);

wait(2);

pEmeter_first = NULL;
pEmeter_last = NULL;

int i;
VECTOR v;
ENTITY* p;
vec_set(v.x, nullvector);


for (i = 0; i < 1000; i ++) {
#ifdef USE_LINK_LIST
p = ent_create(CUBE_MDL,v.x,NULL);
ent_listAdd(p, &pEmeter_first, &pEmeter_last);
#else
p = ent_create(CUBE_MDL,v.x,cube_func);
#endif

}

#ifdef USE_LINK_LIST
while (1) {
wait(1);
ent_listExec(pEmeter_first, cube_rotate);

//remoce cubes...
/*
vec_set(v.x, vector(500, 0, 0));
vec_rotate(v.x, camera.pan);
vec_add(v.x, camera.x);

c_trace(camera.x, v.x, IGNORE_SPRITES);
if (trace_hit) {
draw_point3d(hit.x,vector(0,0,255),100,5);
if ((key_space) && (hit.entity != NULL)) {
ent_listRemove(hit.entity, &pEmeter_first, &pEmeter_last);
ent_remove(hit.entity);
}
}


if (pEmeter_first != NULL) {
draw_point3d(pEmeter_first.x,vector(1,255,255),100,15);
}
if (pEmeter_last != NULL) {
draw_point3d(pEmeter_last.x,vector(255,255,1),100,15);
}
*/

}
#endif

}
Posted By: JibbSmart

Re: performence boost for your project - 06/21/10 13:30

Very clever. I'll consider something like this in the future!

Jibb
Posted By: Lukas

Re: performence boost for your project - 06/21/10 14:04

Wow, this is so much faster than actions? I would have always guessed something like a linked list would be faster than using caroutines, but only like 10fps faster, even for so many entities, and not 40fps. Thanks for the hint! laugh

grin But I couldn't help giggling a bit after seeing this code again. wink
Posted By: ventilator

Re: performence boost for your project - 06/21/10 14:28

it would be interesting to know where the overhead comes from since internally the scheduler also only uses some list with the functions. a wait() also stores/restores the local variables but does this take so much time?
Posted By: Quad

Re: performence boost for your project - 06/21/10 15:01

so this is for running only one fuction for entites with same behaviour instead of running the same action for them all?

if i got it right, it's sounds very logical.

if i got it wrong someone please enlighten me.
Posted By: Lukas

Re: performence boost for your project - 06/21/10 15:04

You either got it right, or we both got it wrong. grin
Posted By: Zapan@work

Re: performence boost for your project - 06/21/10 15:07

You can use many diffrent functions for any kind of entity... Just give it a try. We've checked this withs our old glider game and have ~40 fps in all levels...
Posted By: Joey

Re: performence boost for your project - 06/21/10 20:22

yeah but like that all the advantages of having coroutines are gone, like local variables being restored after a wait.
Posted By: Pappenheimer

Re: performence boost for your project - 06/26/10 09:37

Originally Posted By: Joey
yeah but like that all the advantages of having coroutines are gone, like local variables being restored after a wait.

What sort of coroutines fell off, too?
Lokal variabels are not such a loss, if you have been familiar with programming in wdl, right?
Skills are still valid, as far as I understood this.

The question is, is it worth the hassle in your specific project...
© 2024 lite-C Forums