About Store Forum Documentation Contact
Donations:
169$/mo



Post Reply 
Efficiency: drawing
Author Message
AndrewBGS Offline
Member

Post: #1
Efficiency: drawing
I'm an efficiency obsessed guy, so I have to ask this: how is more efficient to draw an equipped item?

The tutorial had this: if(slot[head].valid())slot[head]().drawPrepare();
And I have this: items[11].drawPrepare();

//without giving too much detail about my code, I basicaly have quite a few null items (items created using null as parameter pointer. They seem to fit my plans perfectly, they don't bug the program and act like real items when i need them to)

So: the drawPrepare function. Is it more time-consuming if the item is a null one? (which I don't really know what is, I don't know how it's build with null parameters). Or it is more efficient to use an IF before it?

I'm strictly talking about efficiency; I'd love an answer from Esenthel as he's probably the only one who knows what's behind those functions and constructors.
05-20-2013 07:29 AM
Find all posts by this user Quote this message in a reply
Esenthel Offline
Administrator

Post: #2
RE: Efficiency: drawing
You should never call any method of a pointer to object which may be null.
05-20-2013 12:19 PM
Find all posts by this user Quote this message in a reply
AndrewBGS Offline
Member

Post: #3
RE: Efficiency: drawing
The pointer itself is never null. The item behind it is. (I'm making sure it IS pointing to an item, that is checked; just that the specified item may be an actual item, or it may be a dummy item I'm using; created using null pointer.
I don't have the code on me, but it's something like:
Game:ObjParamsPtr p=null; item.create(&p);
And that's the kind of items I'm sometimes calling DrawPrepare on.

I'd like to know if doing this is more or less efficient than adding an if to check if the item is a non-dummy item. I'm assuming there is a built-in if in the DrawPrepare?
05-20-2013 12:40 PM
Find all posts by this user Quote this message in a reply
Esenthel Offline
Administrator

Post: #4
RE: Efficiency: drawing
Hello,

You have source code for Item.drawPrepare in Game Obj classes.

If you're interested if it's more efficient to do "if" or to just call function that does almost nothing, you'll get the best answer by making small programs that does 100000 (or more) of both scenarios and measure time.

When it comes to Reference class, like I said before, you should never do () operatator without previously checking the 'valid' method (it's in the comments). Even if you do make sure to set this pointer to some dummy object, then I still encourage to call 'valid' because it's easy to make mistakes if you for example later forget about it, and don't set the pointer to some object, or just copy the codes to some other functions, which don't make assumption of pointer not being null.
In programming you should never "skip" things, unless you're writing super performance critical functions and you really know what you're doing.
05-20-2013 12:56 PM
Find all posts by this user Quote this message in a reply
AndrewBGS Offline
Member

Post: #5
RE: Efficiency: drawing
I'm not using references, the items I'm working on will have a stable spot and I work on the specific spot precisely. I am aware of all the "unorthodox" things I'm making, they keep telling me that at university. But I'm an efficiency junkie and OOP doesn't really consider that. And it's drawing; that function gets called 20-60 times per second, and as a hardware programmer I know IF's are among the most "expensive" operations around, next to multiplication and division. So I'm trying to remove them as much as possible.

Oh on that note, I got to say I LOVE the way you move your character without IF's, just moving it according to keys, without using if's. That's.... that's the kind of thing I love grin

I suppose I should indeed get myself some benchmarking functions to test code. I'll work on that.
05-20-2013 01:12 PM
Find all posts by this user Quote this message in a reply
Rubeus Offline
Member

Post: #6
RE: Efficiency: drawing
An how long does an 'if' really take? If it's run 1,000 times a second, how many FPS are you losing? Is that amount worth the possibility of not checking your code and possibly introducing a bug?

1,000 if statements are 3 ms of processing time, according to my test. This means you would have to have ~6,000 to reduce your FPS by 1 from 60 to 59.

Is it worth having these checks in your code? I'd say yes. Easier to catch bugs, which might help performance more in the long run.
05-20-2013 02:21 PM
Find all posts by this user Quote this message in a reply
AndrewBGS Offline
Member

Post: #7
RE: Efficiency: drawing
True, but that's just one occurrence of my if. A character might have equipped let's say 10 items in average. I might have (or at lest I hope will, if i get this to the point of MMO) around 100 NPCs and characters at a time calling these IFs. (which actually I think they exist in the drawPrepare anyway... have to check). So here's 1000 if's already. I also have similar functionality in other places of inventory. Regardless, I'm trying to optimise everything that can be optimised.

A lot of my system relies on these dummy void items, which seemed to work perfectly so far. And these are the items ON the character, they are checked for validity when they are placed on the character. (that is a "once-in-a-lifetime" check, just when i place it there, not 60 times per second). So.... yeah I feel pretty secure about my improvisation grin
05-20-2013 02:29 PM
Find all posts by this user Quote this message in a reply
Tottel Offline
Member

Post: #8
RE: Efficiency: drawing
If you have a 100 NPC's on screen, a couple of "if"-checks will be the least of your worries for performance. smile
05-20-2013 05:31 PM
Find all posts by this user Quote this message in a reply
AndrewBGS Offline
Member

Post: #9
RE: Efficiency: drawing
Most certainly. But as I said, I improve everything I can. Seems the sensible thing to do. If you can do a thing even easier than damn easy, why not?
05-20-2013 06:18 PM
Find all posts by this user Quote this message in a reply
TBJokers Offline
Member

Post: #10
RE: Efficiency: drawing
If this is Client sided then you don't ever need to worry about that, in this case i would use the if to see if it's valid. But it depends if it is needed, i'm not sure how you programmed yours. Remember that mostly people will have above 60fps with vsync on, and then with it on even if you were to add 6000 if commands then it wouldn't make a difference. There's really no need to worry. For a server you would still have to check these since you only need to check valid on pointers and pointers are critical to use.

On the other hand try to work other stuff out instead of playing ball with if commands, there are calculations etc that will take alot more than an if.
05-20-2013 06:25 PM
Visit this user's website Find all posts by this user Quote this message in a reply
Pixel Perfect Offline
Member

Post: #11
RE: Efficiency: drawing
It's not bad practice to be aware of general efficiency in coding and to put that into practice from the start; however profiling your code is usually the best technique for identifying areas where valuable time is being spent enabling you to concentrate on optimizing those routines.
05-20-2013 07:24 PM
Find all posts by this user Quote this message in a reply
cat555 Offline
Member

Post: #12
RE: Efficiency: drawing
(05-20-2013 12:56 PM)Esenthel Wrote:  Hello,

You have source code for Item.drawPrepare in Game Obj classes.

If you're interested if it's more efficient to do "if" or to just call function that does almost nothing, you'll get the best answer by making small programs that does 100000 (or more) of both scenarios and measure time.

Hi @Esenthel,

Where can i find such code for Item.drawPrepare? In EE i can see only the header files for Game Obj classes...

Thanks!
06-07-2014 03:53 AM
Find all posts by this user Quote this message in a reply
Rubeus Offline
Member

Post: #13
RE: Efficiency: drawing
In the store - go to the engine license you purchased, and it's in the list of downloads. You can find all the game object class source code files there.
06-07-2014 02:38 PM
Find all posts by this user Quote this message in a reply
cat555 Offline
Member

Post: #14
RE: Efficiency: drawing
Hi @Rubeus, thanks for the information smile
06-08-2014 10:32 PM
Find all posts by this user Quote this message in a reply
Post Reply