-7

At the moment i'm trying to optimize my c++ code to get better performance, because I have a strong java/c# background. I've tested simple examples like getter/setter between classes and realized using them with references like this:

vector<string>& MyClass::getNum()
{
   return num;
}

void MyClass::setNum(vector<string> &num)
{
   this->num = num;
}

(let say 15+ Strings are in the Vector)

is faster 2x faster than without. (no difference when using simple/small parameter, but huge diff. when using Vectors etc.)

I also tried the same example in Java, but somehow my Java code is still faster than in C++. Someone can help me?

jeromintus
  • 367
  • 1
  • 5
  • 14
  • 6
    Curious to see your benchmark. – Inisheer Jul 07 '14 at 07:29
  • What optimization level have you been using? – dragosht Jul 07 '14 at 07:31
  • 5
    This question currently lacks a lot of information to be answerable. The only thing answers could currently do is speculate. – Rapptz Jul 07 '14 at 07:32
  • 1
    There should not be a difference between `int&` and `int` performance wise unless your optimization level is too low – jasal Jul 07 '14 at 07:32
  • @jasal He said the difference was visible mainly with big structure like vectors. – Holt Jul 07 '14 at 07:33
  • @Holt Right, I missed that. – jasal Jul 07 '14 at 07:34
  • @Sleicreider Does your code using reference in C++ run faster than the Java one? – Holt Jul 07 '14 at 07:35
  • @Holt he never said such thing. He said "when using vectors" which is a meaningless statement. That *may* mean that you replace `int` with `vector`, but it could also mean that he's iterating over a vector and getting every element. This question is simply too vague. – Adam Jul 07 '14 at 07:35
  • edited my code. int and &int is no difference = true (like I wrote in the text) but using vector with data = big difference. – jeromintus Jul 07 '14 at 07:42
  • 1
    We cannot review code we don't see. – n. m. could be an AI Jul 07 '14 at 07:44
  • There's a simple answer to this... other languages do implicit reference counting of complex objects, but in C++ you'd need to use a `std::shared_pointer` to do this. Both versions of the code in the question do some full-vector-data copying, so they're naturally slower. (Answers that limit themselves to by-value, references and pointers will necessarily make compromises about ownership management / lifetimes that get much more complicated to explain.) – Tony Delroy Jul 07 '14 at 08:00
  • using pointers instead of references, seems to be faster – jeromintus Jul 07 '14 at 10:27

2 Answers2

2

In java, everything is pointer, so when you do this in java:

void function (ArrayList <Integer> myList);

It's almost equivalent in C++ to:

void function (std::vector <int> & myList);
void function (std::vector <int> * myList);

This answer your question on why Java is faster when not using reference in C++, because references are faster in most cases. Then, why does it improve performance?

Well, imagine you have a vector of int, if you pass it to a function without reference, you have to make a copy of the object (call the copy constructor), which take (in general) a huge amount of time since you have to allocate a new array, etc. Another example with a struct:

struct X {
    int a[100] ;
    int b[100] ;
} ;

This struct take something like 2 * 100 * 4 = 800 bytes on a modern machine (may vary of course), so if you pass it to a function like that:

void function (X x) ;

When you call the function, you need to make a copy, so a copy of 800 bytes, if you do:

void function (X const& x) ;

You only pass a reference to the object, i.e. something pointing to the object, which will take (on a x64 architecture), 8 bytes.

Holt
  • 36,600
  • 7
  • 92
  • 139
  • 1
    He never asks "Why there are no difference in C# and Java?". He's asking why his Java code is faster than his C++ code. Nothing about C#. – Adam Jul 07 '14 at 07:36
  • I've tried to use function parameter with reference, no difference at all. – jeromintus Jul 07 '14 at 07:40
  • I've also found this text somewhere: Because references are typically implemented by C++ using pointers, and dereferencing a pointer is slower than accessing it directly, accessing values passed by reference is slower than accessing values passed by value. – jeromintus Jul 07 '14 at 07:46
1

this->num = num; : this line copies the vector you passed by reference. If your goal is to give the vector to MyClass and then forget about it, consider passing it by rvalue reference and moving it.

Quentin
  • 62,093
  • 7
  • 131
  • 191
  • Or, much better, pass it by value and then move it. – Puppy Jul 07 '14 at 08:41
  • @Puppy Won't that also make a copy ? – Quentin Jul 07 '14 at 08:44
  • @Quentin no. Passing by value invokes move constructor if possible. All in all, if you want to *store* something, you need an *instance* of that something, and the "most outside" way to place that instance is in parameter list. Refer to [this question of mine](http://stackoverflow.com/questions/10836221/should-i-write-constructors-using-rvalues-for-stdstring). – Bartek Banachewicz Jul 07 '14 at 08:54
  • @BartekBanachewicz so if my understanding is correct, passing by value means that I get either a copy of the argument or the argument itself, depending on whether the caller wants to keep it or not ? Neat ! – Quentin Jul 07 '14 at 08:58