简体   繁体   English

在 C++ 中使用指针会减少运行时间?

[英]Using Pointers in C++ decreases runtime?

I have a simple question that whether only using pointers instead of normal variables increase the efficiency of the program either time wise or memory wise?我有一个简单的问题,即仅使用指针而不是普通变量是否会在时间或内存方面提高程序的效率? For an instance if I use following program to swap two integers.例如,如果我使用以下程序交换两个整数。

#include<iostream>
#include<conio.h>
#include<new>
using namespace std;
int main()
{
   int *a=new int;
   int *b=new int;
   int *c=new int;
   cin>>(*a)>>(*b);
   *c=*a;*a=*b;*b=*c;
   cout<<"swapping";
   cout<<*a<<*b;
getch();
}

Using pointers to variables instead of variables is unlikely to improve performance.使用指向变量的指针而不是变量不太可能提高性能。 Write the code in clearest way possible and let the compiler optimize the code for you.以尽可能清晰的方式编写代码,让编译器为您优化代码。 If anything, using pointers is likely to slow things down as it makes the compiler analysis harder.如果有的话,使用指针可能会减慢速度,因为它会使编译器分析更加困难。

For large objects, it is worth keeping pointers to objects instead of copying those objects around.对于对象,值得保留指向对象的指针而不是复制这些对象。 Maybe that is the kernel of truth from which you are incorrectly extrapolating.也许这就是您错误推断的真相的核心。

In the example given above, it is less efficient both time wise and memory wise.在上面给出的示例中,它在时间和内存方面的效率都较低。

  • Dynamic allocation cost more time than local variables (there are function calls and work to be done, local variables are created by substracting from a stack pointer, often just one operation for all local variables).动态分配比局部变量花费更多的时间(有函数调用和工作要做,局部变量是通过从堆栈指针中减去来创建的,通常只对所有局部变量进行一次操作)。
  • Dynamic allocation cost more memory than local variables (there are supporting data structures in the memory manager).动态分配比局部变量消耗更多的内存(内存管理器中有支持的数据结构)。

Pointers can make the program more efficient when they are used to prevent copying large structures.当指针用于防止复制大型结构时,它们可以使程序更高效。 int s don't fall in this category. int不属于这一类。

The only thing I can think of that might make using a pointer slower than using a local variable is that the generated assembly might involve more complex memory addressing, thus resulting in larger machine op-codes, which in turn would take ever-so-slightly more time to execute.我能想到的唯一可能使使用指针比使用局部变量慢的事情是生成的程序集可能涉及更复杂的内存寻址,从而导致更大的机器操作码,而这反过来又会花费很少的时间更多的时间来执行。

That time difference would be so negligible though that you shouldn't worry about it.尽管您不必担心,但该时差可以忽略不计。

What you should consider is that allocation on the stack is much faster than allocation on the heap .您应该考虑的是, 堆栈上的分配比堆上的分配快得多 In other words:换句话说:

int* a = new int;

is slower than:慢于:

int a;

but only because of the allocation new int , not because you are using a pointer.但仅仅是因为分配new int ,而不是因为您使用的是指针。

This code does almost nothing at the level where you wrote it.这段代码在您编写它的级别上几乎没有任何作用 If you step through it at the instruction level, you will see that each call to new executes 100s of instructions, and the cout << calls do even far more.如果您在指令级别逐步执行它,您将看到对new每次调用执行 100 条指令,而cout <<调用执行的操作甚至更多。

That's what you're measuring.这就是你测量的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM