What could I improve upon?

closed account (3bfGNwbp)
http://pastebin.com/04utKxbt

What could I improve upon?
Aside from the system call, which I will remove soon.

Thanks guys. :blackhat:
closed account (3bfGNwbp)
Hello?
Hi :)
You could put using namespace std after the includes, that saves you a lot of std::'s.
instead of the std::endl use the \n newline function and occasionally a std::endl

They are not exactly the same, std::endl flushes the cache as well, while \n does not.
Essentially, it makes little sense to flush the cache so many times.

For the rest, I don't see to many things you can improve.
Last edited on
closed account (3bfGNwbp)
\n isn't a function, it's an escape sequence. But yeah, I should use that more often.

I would use the whole namespace but I prefer just typing out the prefix.

Is there anything else that could make my code more efficient?
closed account (zb0S216C)
TheBestHacker wrote:
"Is there anything else that could make my code more efficient?"

Yes:

1) Place member function definitions within a source file, and not within the class itself. This is because member functions declared and defined inside a class are implicitly declared in-line. In-lining can improve performance by increasing the number of CPU cache hits. However, excessive in-lining can cause thrashing, and can increase the number of CPU cache misses.

2) Declare parameters/variables/objects constant if you plan to only read from them.
3) Use iterators to transverse containers, rather than using indices. Here's an example:

1
2
3
4
std::vector<int> v_;

for(std::vector<int>::iterator i_(v_.begin()); i_ < v_.end(); ++i_)
    *i_ = /*...*/;

Wazzak
closed account (3bfGNwbp)
Here's my new and improved code: http://pastebin.com/fNvYdu5G

1) What if I do something like this:
1
2
3
4
5
6
7
8
9
10
11
12
class Shape {
    Shape();
    void getData();
};

Shape::Shape() {

}

void getData() {

}

Does that make that constructor and method inline? I don't like putting them in other files when it's only a small bit of code.

2) Did I not do that?

3) Done!
closed account (zb0S216C)
TheBestHacker wrote:
"Does that make that constructor and method inline?"

No, unless you explicitly declare them in-line during their definition.

TheBestHacker wrote:
"I don't like putting them in other files when it's only a small bit of code."

For small 1-line functions, it's OK, but not for large functions.

2) Not in your constructor.

Wazzak
closed account (3bfGNwbp)
Ah, okay I see I'll put the bigger functions outside of the class.

2) I'll change that right away!

And while you're here, I'd like to ask you this; When should I use inline functions?
closed account (zb0S216C)
A good candidate would:

- ...have a very short body (1-line; 2 max)
- ...perform no intensive operations (I/O)
- ...be invoked (called) frequently.

You should note that a function declared in-line, may not actually be in-lined; inline is just a hint to the compiler, not a command.

Wazzak
Last edited on
closed account (3bfGNwbp)
Oh, I thought it told the compiler to replace every function call with the code inside of that function.

So in my case I shouldn't have any inline functions..
closed account (zb0S216C)
TheBestHacker wrote:
"Oh, I thought it told the compiler to replace every function call with the code inside of that function."

That's exactly what inline hints the compiler to do.

TheBestHacker wrote:
"So in my case I shouldn't have any inline functions.."

I can see multiple possible candidates for in-lining in your code.

Wazzak
closed account (3bfGNwbp)
These are the only two I would think of in-lining.

1
2
void attackPlayer(Character *b);
void attachWeapon(const Weapon *a);


The rest I would use a lot.
Last edited on
closed account (zb0S216C)
attackPlayer() is a possible candidate, but the call to std::vector::push_back() in attachWeapon() might might be enough to convince the compiler not to in-line attachWeapon().

Wazzak
1
2
3
Character *hero = new Character(ch == 1?"Gandorf":"Ashe", ch == 1?70:100, ch == 1?40:20);
//...
delete hero;
sigh, ¿why dynamic allocation?
And that creation is quite obfuscated.

Also, get get get. ¿do you need so many things?

¿Why is _it (clearly an aux for the print method) a member of the class?

Edit:
gameRun != false gameRun is already a Boolean. ¿why don't just check its value?
Last edited on
Topic archived. No new replies allowed.