Performance of Pointer vs Index in C++

When we write an array assignment statement in C++, there are two different approach:

[code lang=cpp]
for(double *pd=dd, *pEnd=&dd[rc]; pd<pEnd; pd++) *pd = f;
[/code]

or:

[code lang=cpp]
for(int i=1; i<rc; ++i) dd[i] = f;
[/code]

Is there any difference in Performance between two way? The conclusion is: Using Pointer(First approach) not never worse than Using Index(Second approach). After compiler optimization, both performance are equal.

Here are my g++(version 8.2.1) test result:

  1. debug mode(-g): Using Index is 15% slower than Using Pointer in average;
  2. -O mode: Using Index is 80% slower than Using Pointer in average;
  3. -O2 mode: Both performance are equal;
  4. -O3 mode: Both performance are equal;

Main test Source Code:

[code lang=cpp]
CVect CVect::operator +(const CVect &v)
{
CVect tmp = v;
auto start1 = std::chrono::system_clock::now();
for(int j = 0; j < 10000; j++)
{
for(int i = 0; i < this->m_Size; ++i)
tmp.data[i] = this->data[i] + v.data[i];
}
auto end1 = std::chrono::system_clock::now();
auto elapsed1 = end1 – start1;
printf("Using Index: %li ticks\n",elapsed1.count());

auto start = std::chrono::system_clock::now();
for(int j = 0; j < 10000; j++)
{
double *pout = tmp.data;
double *pthis = this->data;
double *pv = v.data;
for(double *PEnd = &tmp.data[tmp.m_Size]; pout < PEnd ; )
*pout++ = *pthis++ + *pv++;
}
auto end = std::chrono::system_clock::now();
auto elapsed = end – start;
printf("Using Pointer: %li ticks\n",elapsed.count());
printf("Index/Pointer: %.3f \n",double(elapsed1.count())/double(elapsed.count()));
return tmp;
}
[/code]

Test Result1 < Debug mode >

[code lang=text]
> g++ -g test.cpp && ./a.out
Using Index: 3688867 ticks
Using Pointer: 3064518 ticks
Index/Point: 1.204
> g++ -g test.cpp && ./a.out
Using Index: 4256750 ticks
Using Pointer: 3364219 ticks
Index/Pointer: 1.265
> g++ -g test.cpp && ./a.out
Using Index: 3675937 ticks
Using Pointer: 3071578 ticks
Index/Pointer: 1.197
[/code]

Test Result

[code lang=text]
> g++ -O1 test.cpp && ./a.out
Using Index: 1697346 ticks
Using Pointer: 941173 ticks
Index/Pointer: 1.803 ticks
> g++ -O1 test.cpp && ./a.out
Using Index: 1621724 ticks
Using Pointer: 926362 ticks
Index/Pointer: 1.751
> g++ -O1 test.cpp && ./a.out
Using Index: 1646057 ticks
Using Pointer: 955308 ticks
Index/Pointer: 1.723
[/code]

Test Result

[code lang=text]
> g++ -O2 test.cpp && ./a.out
Using Index: 1003813 ticks
Using Pointer: 918925 ticks
Index/Pointer: 1.092
> g++ -O2 test.cpp && ./a.out
Using Index: 1007096 ticks
Using Pointer: 1035870 ticks
Index/Pointer: 0.972
> g++ -O2 test.cpp && ./a.out
Using Index: 988068 ticks
Using Pointer: 1001220 ticks
Index/Pointer: 0.9871
[/code]

Test Result

[code lang=text]
> g++ -O3 test.cpp && ./a.out
Using Index: 475984 ticks
Using Pointer: 464027 ticks
Index/Pointer: 1.026
> g++ -O3 test.cpp && ./a.out
Using Index: 444801 ticks
Using Pointer: 441412 ticks
Index/Pointer: 1.008
> g++ -O3 test.cpp && ./a.out
Using Index: 440249 ticks
Using Pointer: 453357 ticks
Index/Pointer: 0.971
[/code]

Full Test Source Code download

此条目发表在CODING分类目录,贴了标签。将固定链接加入收藏夹。

发表评论

电子邮件地址不会被公开。

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据