根據GNU 檔案:
int square (int) __attribute__ ((const))告訴 GCC 對具有相同引數值的函式 square 的后續呼叫可以被第一次呼叫的結果替換,而不管中間的陳述句如何。
__attribute__((const))我希望以下代碼在函式宣告中洗掉時會變慢。
#include <stdio.h>
#include <limits.h>
int my_double(int b) __attribute__((const));
//int my_double(int b);
int main(void) {
long result = 0;
for (int i = 0; i < INT_MAX/2; i )
{
result = my_double(5);
}
printf("%ld\n", result);
}
int my_double(int b) {
return b*2;
}
然而,實驗表明,__attribute__((const))不會顯著影響時序結果。有誰知道原因?謝謝。
順便說一句,我使用以下命令清除任何可能污染每個實驗的計時結果的快取。
sudo sh -c 'echo 3 > /proc/sys/vm/drop_caches'
sudo swapoff -a
sudo swapon -a
并用于/usr/bin/time計時實驗。
PS。對應的匯編如下:(我對匯編不熟悉)
.file "attribute-o.c"
.text
.section .rodata
.LC0:
.string "%ld\n"
.text
.globl main
.type main, @function
main:
.LFB0:
.cfi_startproc
endbr64
pushq %rbp
.cfi_def_cfa_offset 16
.cfi_offset 6, -16
movq %rsp, %rbp
.cfi_def_cfa_register 6
subq $16, %rsp
movq $0, -8(%rbp)
movl $0, -12(%rbp)
jmp .L2
.L3:
movl $5,
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/432695.html
