假设我们有一个 A 类:
template<int N>
class A final{
public:
void foo() const { cout << N << endl; }
};
Run Code Online (Sandbox Code Playgroud)
模板参数 N 存储在哪里?在堆中还是在堆栈中的对象内存中?
我有一个非常简单的程序来测量一个函数花费了多少时间。
#include <iostream>
#include <vector>
#include <chrono>
struct Foo
{
void addSample(uint64_t s)
{
}
};
void test(const std::vector<uint64_t>& samples)
{
uint32_t onlyCallTime = 0;
uint32_t loopOnlyTime = 0;
Foo stats;
std::chrono::high_resolution_clock::time_point callStart,callEnd;
auto start = callStart = callEnd = std::chrono::high_resolution_clock::now();
for(auto &s : samples)
{
callStart = std::chrono::high_resolution_clock::now();
loopOnlyTime += std::chrono::duration_cast<std::chrono::microseconds>(callStart-callEnd).count();
stats.addSample(s);
callEnd = std::chrono::high_resolution_clock::now();
onlyCallTime += std::chrono::duration_cast<std::chrono::microseconds>(callEnd-callStart).count();
}
auto end = std::chrono::high_resolution_clock::now();
std::cout << "overall duration: " << std::chrono::duration_cast<std::chrono::microseconds>(end-start).count() << std::endl;
std::cout << "only call duration: " << …Run Code Online (Sandbox Code Playgroud) So I was messing around with inline assembly and compiled this using GCC 9. The result was that the two variables a and b were swapped without actually issuing any direct commands.
#include<cstdio>
int main(){
int a(1),b(2),c(3);
asm ("": "=r"(c):"r"(a));
asm ("": "=r"(a):"r"(b));
asm ("": "=r"(b):"r"(c));
printf("%d %d %d", a,b,c);
}
Run Code Online (Sandbox Code Playgroud)
Can somebody explain what is going on here?
c++ ×3
assembly ×1
benchmarking ×1
c++-chrono ×1
duration ×1
gcc ×1
heap-memory ×1
parameters ×1
templates ×1