• There is a std :: vector, in each of them there are still vectors, in each of them there are also vectors.
  • Case 0: In the most recent nested vector, there are numbers 1 byte in size, with options of values: 0,1,2. vector<unsigned char> v; The comparison itself: if (char0 == char1)
  • Case 1: In the most recent nested vector are pointers to numbers 1 byte in size. Variants of values: 0,1,2., But only pointers to them are stored in vector. The comparison itself: if (ptr_char0 == ptr_char1)

    int i0 = 0; int i1 = 1; int i2 = 2; vector<unsigned char*> v;

  • Case 2: similar to case 1, but will be taken to compare the values ​​on the pointers. The comparison itself: if (*ptr_char0 == *ptr_char1)

  • The lengths of each "level" vector are different and change over time. It is necessary to go through all the elements and make a comparison (only for equality). Delete something, change something, add elements or vector somewhere, but the action pattern (if-else) is the same for both cases.

Will it be faster to compare variables or pointers, or take values ​​for comparison by pointers, or some other variant?

The following example showed that the fastest case is # 2, which, on the contrary, should be the slowest way.

  int n0=10; int n1=20; int *p0 = &n0; int *p1 = &n1; int *q0 = &n0; int *q1 = &n1; int time0 = clock(); for (int i=0;i!=25500000;++i) if (n0==n1){} int time1 = clock(); for (int i=0;i!=25500000;++i) if (p0==p1){} int time2 = clock(); for (int i=0;i!=25500000;++i) if (*q0==*q1){} int time3 = clock(); printf("%i %i %i %i",time0,time1,time2,time3); 

Exhaust: 311 312 313 313

Obviously, the load is too small in this example, it is difficult to estimate.

  • one
    Well, write a program and experiment. Any theoretical reasoning without experiment is worthless. And the easiest way is not to run along vectors, but simply to compare two external equations for equality - they will figure out how to compare element by element ... - Harry
  • one
    @ Gleb Compare pointers does not make sense. Comparing the values ​​themselves is obviously at least not slower than using reference pointers to values ​​and dereferencing them for comparison. - Vlad from Moscow
  • @Gleb This is provided that the pointers point to the same element and lie in the range of the array. - Vlad from Moscow
  • In general, judging by the question - to compare only the values. - pavel
  • @ Gleb: So what if you have int a = 123, b = 123; int *p = &a, *q = &b; int a = 123, b = 123; int *p = &a, *q = &b; . What do you want to get in this case when comparing p and q and how are you going to get by with "comparing pointers"? - AnT

1 answer 1

If in the context of your task the equivalence is postulated:

 равные указатели <=> равные указуемые значения 

Of course, in the general case, it would be more profitable to simply compare pointers, instead of redirecting pointers, read specific values ​​from memory, and then compare values.

It should be remembered that relative comparisons of arbitrary pointers are supported only through std::less and other standard predicates of relative comparisons, but not through the built-in operators < , > , etc. The latter allow comparison only of pointers pointing to the sub-objects of a single object with a number of additional restrictions.

For comparison operators of pointers to equality == and != There are no such restrictions.