B
Brian Genisio
Hello all,
I am developing a class where speed is important, and memory size is
also important. I need to give the user of the class read-only access
to one of the private members (size as an int). I have two options:
1. Make a method : int getSize() {return m_size;}
2. Use a reference: const int &size;, and link it to the private size value
Option #1 is nice, because it does not take an extra member, but it is
slower.
Option #2 is nice, because it is faster than a method, but it takes 4
bytes per instance of the class
In a non-optimized mode, option 1 is 2 times slower than option 2 (from
some simple tests I did). On the other hand, in optimized mode, I did
not notice a difference, as far as I can tell... but I think my
optimizer was optimizing a lot more, so I am not sure that my test was
accurate.
What do you think? What is the most EFFICIENT way to do things?
Usability and maintenance is not much of a concirn for me, since
efficiency is much more important.
Thanks,
Brian
I think I gave enough info, but in case you want to see the code, here
is the test application.
#include <iostream.h>
#include "timer.h"
class Tester
{
public:
int getSize() { return m_size; }
const int &size;
Tester() : m_size(0), size(m_size) {}
Tester(int setter) : m_size(setter), size(m_size) {}
private:
int m_size;
};
#define LOOP_SIZE 10000000
int main(void)
{
int i;
Tester my_test(20);
int temp_size;
BGTimer timer;
timer.start();
for(i=0; i<LOOP_SIZE; i++)
temp_size = my_test.getSize();
timer.end_and_print();
timer.start();
for(i=0; i<LOOP_SIZE; i++)
temp_size = my_test.size;
timer.end_and_print();
return 0;
}
I am developing a class where speed is important, and memory size is
also important. I need to give the user of the class read-only access
to one of the private members (size as an int). I have two options:
1. Make a method : int getSize() {return m_size;}
2. Use a reference: const int &size;, and link it to the private size value
Option #1 is nice, because it does not take an extra member, but it is
slower.
Option #2 is nice, because it is faster than a method, but it takes 4
bytes per instance of the class
In a non-optimized mode, option 1 is 2 times slower than option 2 (from
some simple tests I did). On the other hand, in optimized mode, I did
not notice a difference, as far as I can tell... but I think my
optimizer was optimizing a lot more, so I am not sure that my test was
accurate.
What do you think? What is the most EFFICIENT way to do things?
Usability and maintenance is not much of a concirn for me, since
efficiency is much more important.
Thanks,
Brian
I think I gave enough info, but in case you want to see the code, here
is the test application.
#include <iostream.h>
#include "timer.h"
class Tester
{
public:
int getSize() { return m_size; }
const int &size;
Tester() : m_size(0), size(m_size) {}
Tester(int setter) : m_size(setter), size(m_size) {}
private:
int m_size;
};
#define LOOP_SIZE 10000000
int main(void)
{
int i;
Tester my_test(20);
int temp_size;
BGTimer timer;
timer.start();
for(i=0; i<LOOP_SIZE; i++)
temp_size = my_test.getSize();
timer.end_and_print();
timer.start();
for(i=0; i<LOOP_SIZE; i++)
temp_size = my_test.size;
timer.end_and_print();
return 0;
}