Object or Vector?

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
Post Reply
LunaRebirth
Posts: 386
Joined: Sun May 11, 2014 12:13 am

Object or Vector?

Post by LunaRebirth »

Hello,
I had a question about which is more efficient to use.

This isn't related to Irrlicht in any way, just a question.

Would it be more efficient to, for example, do this?:

Code: Select all

vector<int> vec;
int main() {
   for (int x = 0; x < 10; x++)
      vec.push_back(x);
   int tryAll=0;
   while (true) {
      if (vec[tryAll]==3) {
         cout << "Yes!" << endl;
      }
      tryAll++;
      if (tryAll>10) tryAll=0;
   }
}
Or

Code: Select all

public class Tester {
   public Tester(int val2) {
      val=val2;
   }
   public main() {
      if (val==3)
         cout << "Yes!" << endl;
}
public int val;
 
//main for program
vector<Tester> vec;
int main() {
   for (int x = 0; x < 10; x++)
      vec.push_back(new Tester(x));
   int tryAll=0;
   while (true) {
      vec[tryAll].main();
      tryAll++;
      if (tryAll>10) tryAll=0;
   }
}
(Code not tested, Sorry if there are any errors)

I'm wondering which direction I should go.
I've created a world editor on Irrlicht and I'm doing vectors for objectNode, objectType, objectAnim, objectHide, objectShowOnMap, etc.
But was wondering if I should create a class and just make my world objects as actual Objects rather than vectors.

What would you guys suggest?
hendu
Posts: 2600
Joined: Sat Dec 18, 2010 12:53 pm

Re: Object or Vector?

Post by hendu »

The object one has extra allocations and extra function calls. May be worth it if your code is more complex though.
CuteAlien
Admin
Posts: 9734
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Re: Object or Vector?

Post by CuteAlien »

Why don't you test your code? You can do that even online: http://ideone.com/ (switch to c++ on input).
I usually do run g++ on the console for quick 1-file tests (no project setup etc - just call "g++ myfile.cpp").

First bug - in your second case you don't set 10 different values. You just overwrite the same one 10 times :-) I assume you meant to have one member variable in Tester.
Also you can't do a "new Tester" for stack objects (that would return a pointer...). So let's give 2 answers:

When your vector looks as declared then you would do: vec.push_back(Tester(x));
In debug the compiler would add a function call for Tester::main which would slow down things a little bit. In release with optimization it would likely inline that away and the result would be the same. So it's probably just as fast (but certainly up to the compiler what to do with it).

As second answer let's use pointers (vector<Tester*>) and new. The new allocation would have a one-time cost which is noticeable. Memory allocations/deallocations are the one thing you should do always ahead in games (at level-loading time for example) so they don't occur while the game runs.
They are expensive.
There is a second cost which also matters (but often can't be avoided as it's a matter of runtime vs flexibility). Your function call would now be "vec[tryAll]->main();". So your array would contains pointers instead of the memory of the Tester member variable (which I still assume you wanted to add). And that messes with the way modern CPUs work. When a CPU requests memory it always requests a whole buffer, not just a single byte and they put it all in their cache. So when all values are behind each other like in the first example all checks will use the CPU cache. In the second example the pointers are cached, but for each pointer it has now to get the memory where it points at. And that memory is not necessarily close together so the CPU might have to refill the cache in between. And this can be expensive. Not as expensive as a memory allocation, but it still matters as that kind of code is generally run in speed-critical parts of the program (this is actually one of the major problems in the Irrlicht scene-graph architecture, but don't tell anyone).
IRC: #irrlicht on irc.libera.chat
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
LunaRebirth
Posts: 386
Joined: Sun May 11, 2014 12:13 am

Re: Object or Vector?

Post by LunaRebirth »

CuteAlien wrote:Why don't you test your code? You can do that even online: http://ideone.com/ (switch to c++ on input).
I usually do run g++ on the console for quick 1-file tests (no project setup etc - just call "g++ myfile.cpp").

First bug - in your second case you don't set 10 different values. You just overwrite the same one 10 times :-) I assume you meant to have one member variable in Tester.
Also you can't do a "new Tester" for stack objects (that would return a pointer...). So let's give 2 answers:

When your vector looks as declared then you would do: vec.push_back(Tester(x));
In debug the compiler would add a function call for Tester::main which would slow down things a little bit. In release with optimization it would likely inline that away and the result would be the same. So it's probably just as fast (but certainly up to the compiler what to do with it).

As second answer let's use pointers (vector<Tester*>) and new. The new allocation would have a one-time cost which is noticeable. Memory allocations/deallocations are the one thing you should do always ahead in games (at level-loading time for example) so they don't occur while the game runs.
They are expensive.
There is a second cost which also matters (but often can't be avoided as it's a matter of runtime vs flexibility). Your function call would now be "vec[tryAll]->main();". So your array would contains pointers instead of the memory of the Tester member variable (which I still assume you wanted to add). And that messes with the way modern CPUs work. When a CPU requests memory it always requests a whole buffer, not just a single byte and they put it all in their cache. So when all values are behind each other like in the first example all checks will use the CPU cache. In the second example the pointers are cached, but for each pointer it has now to get the memory where it points at. And that memory is not necessarily close together so the CPU might have to refill the cache in between. And this can be expensive. Not as expensive as a memory allocation, but it still matters as that kind of code is generally run in speed-critical parts of the program (this is actually one of the major problems in the Irrlicht scene-graph architecture, but don't tell anyone).
Thanks, but I was just asking for the sake of the question. This will actually be in Java, though I had assumed classes worked similarly in C++.

I submitted a contest in California, a 2D game, and placed for a national competition being held in Chicago this summer.
After the judges had "lost" my score sheet, they'd finally emailed me a PDF of my scores - to allow me to see what I could improve on (since I will be competing against 149 other teams and I am an individual programmer [Not that it matters, but we had 4 months to make a full game; I got the news late, created a full game in 13 days, and still won against many others]).

My lowest score was a 6/10 on programming logic. I assumed this was because instead of creating objects per "enemy" or "bullet" being shot, I was using one main class with everything being vectors.
Instead of trying to fix everything up, I decided to entirely re-write the game code (we are given 7 days to fix errors, change our code, or add extra content. I have 5 days left).
With that being said, instead of having a method named "AllBullets" and looping through each vector of bullets and changing their position, I've now created a class named "Bullets" and going to loop through each bullet calling a bullet.main() method.

I figured that now that I have a vector of bullets, then each bullet vector is being called in a loop (rather than having a ton of vectors in one main class and looping through each in that same class), this may change performance for the better or worse.
Finding the answer to my question will not change the way I'm going to code. It was just something I was thinking about and Google wouldn't help me out.

Hopefully you can read the above and understand, I'm sorry if I explained some things too difficult to interpret my full question.

Thanks for your help!
Am I doing a good thing re-writing my code using objects instead of vectors of variables?
CuteAlien
Admin
Posts: 9734
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Re: Object or Vector?

Post by CuteAlien »

Sorry, not enough experience with Java to make assumptions about speed there. But it's probably not a big difference anyway.

And please don't call class memberfunctions "main". There should only be one main function in your application (and maybe a wrapper with the same name in special situations). It works ... it's just confusing to experienced programmers as we always talk about _the_ main function and don't expect there to be more of them ;-)

Good luck with the competition.
IRC: #irrlicht on irc.libera.chat
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
Post Reply