将可选参数传递给方法而不是计算它的成本

Gra*_*Lup 0 c++ memory optimization performance

我有一个内存块,它被分成一系列位置,可以通过客户端代码检索和返回.
返回位置的方法如下所示:

void ReturnLocation(void *address) {
    int location = AddressToLocation(address); // I need the location here
    // some code
    DoSmthA(location);
}

void DoSmthA(int location) {
    // I need the address, but also the location
    void *address = LocationToAddress(location); 
    // do something with the address
    DoSmthB(location);
}

void DoSmthB(int location) {
    // Again, I need the address, but also the location
    void *address = LocationToAddress(location);
    // do something with the address
    DoSmthC(location); // It may go on this way...
}

// ------------------------------------------------------
void* LocationToAddress(int location)
{
    return (void *)((char *)this + HEADER_SIZE + (location * LocationSize));
}

int AddressToLocation(void *address)
{
    return (int)(((__int64)address - HEADER_SIZE - (__int64)this) / LocationSize);
}  
Run Code Online (Sandbox Code Playgroud)

我的问题是:我应该只将位置传递给辅助方法,还是每次传递地址的速度更快(而不是一次又一次地计算):

void DoSmthA(int location, void *address) { }  
Run Code Online (Sandbox Code Playgroud)

甚至更好地使用这样的结构:

struct LocationInfo { int Location; void *Address; };
void DoSmthA(LocationInfo locInfo) { }
Run Code Online (Sandbox Code Playgroud)

这种方法可能被称为百万次,我不确定从位置计算地址的操作(两次加法和一次乘法)是否比传递包含地址的第二个参数更快或更慢.

提前致谢!

jal*_*alf 8

简介它.在您的情况下,在编译器和代码库中执行实际上更快的操作.在我无关的编译器中,不是我的无关测试中的速度更快.

将参数传递给函数是一种非常便宜的操作.基本上是一个堆栈推/弹.

如果可以优化除法,则计算位置可能非常快(取决于LocationSize的值,以及它是否在编译时已知).

所以尝试两者,看看哪个在现实世界中更快.

CPU是复杂的野兽,性能并非微不足道.