是否可以在运行时从C代码中完全删除函数调用,并在需要时将其插回。
我不确定是否可以在运行时修改ELF,这样就不会浪费cpu周期而不使用函数。
我不想在函数调用之前放置'if'检查以避免调用函数。
例如,如果全局标志g_flg = 1,则func1应如下所示
void func1(int x)
{
/* some processing */
func2(y);
/* some processing */
}
如果全局g_flag = 0,则func1应如下所示
void func1(int x)
{
/* some processing */
/* some processing */
}
答案 0 :(得分:2)
不要优化不需要它的东西。您是否尝试过评估绩效的潜在改进?
尝试将g_flg设置为1并执行以下操作:
if (g_flg == 1) {func2(y);}
然后尝试执行此操作:
func2(y);
两百万次(或者在合理的时间内可以运行它的任何次数)。我很确定你会注意到两者之间几乎没有区别。
另外,除此之外,我认为你想做的事情是不可能的,因为ELF是一种二进制(编译)格式。
答案 1 :(得分:1)
你可能会做的事情就是这样:
struct Something;
typedef struct Something Something;
int myFunction(Something * me, int i)
{
// do a bunch of stuff
return 42; // obviously the answer
}
int myFunctionDoNothing(Something * dummy1, int dummy2)
{
return 0;
}
int (*function)(Something *, int) = myFunctionDoNothing;
// snip to actual use of function
int i;
function = myFunctionDoNothing;
for (i = 0; i < 100000; ++i) function(NULL, 5 * i); // does nothing
function = myFunction;
for (i = 0; i < 100000; ++i) function(NULL, 5 * i); // does something
这可能是过早优化。根据编译器如何处理这个以及你的cpu如何处理分支,你实际上可能会以这种方式失去性能而不是天真的方式(在带有标志的函数中停止它)
答案 2 :(得分:0)
在大多数桌面和服务器架构上,分支比间接调用更快,因为它们进行分支预测和/或推测执行。我从来没有听说过间接调用比单个分支更快的架构。 (对于switch()
语句,跳转表有多个分支,因此完全不同。)
考虑下面我把它们放在一起的微基准测试。 test.c
:
/* test.c */
volatile long test_calls = 0L;
volatile long test_sum = 0L;
void test(long counter)
{
test_calls++;
test_sum += counter;
}
work.c
:
/* work.c */
void test(long counter);
/* Work function, to be measured */
void test_work(long counter, int flag)
{
if (flag)
test(counter);
}
/* Dummy function, to measure call overhead */
void test_none(long counter __attribute__((unused)), int flag __attribute__((unused)) )
{
return;
}
和harness.c
:
#define _POSIX_C_SOURCE 200809L
#include <unistd.h>
#include <stdlib.h>
#include <time.h>
#include <stdint.h>
#include <string.h>
#include <stdio.h>
/* From test.c */
extern volatile long test_calls;
extern volatile long test_sum;
/* Dummy function, to measure call overhead */
void test_none(long counter, int flag);
/* Work function, to be measured */
void test_work(long counter, int flag);
/* Timing harness -- GCC x86; modify for other architectures */
struct timing {
struct timespec wall_start;
struct timespec wall_stop;
uint64_t cpu_start;
uint64_t cpu_stop;
};
static inline void start_timing(struct timing *const mark)
{
clock_gettime(CLOCK_REALTIME, &(mark->wall_start));
mark->cpu_start = __builtin_ia32_rdtsc();
}
static inline void stop_timing(struct timing *const mark)
{
mark->cpu_stop = __builtin_ia32_rdtsc();
clock_gettime(CLOCK_REALTIME, &(mark->wall_stop));
}
static inline double cpu_timing(const struct timing *const mark)
{
return (double)(mark->cpu_stop - mark->cpu_start); /* Cycles */
}
static inline double wall_timing(const struct timing *const mark)
{
return (double)(mark->wall_stop.tv_sec - mark->wall_start.tv_sec)
+ (double)(mark->wall_stop.tv_nsec - mark->wall_start.tv_nsec) / 1000000000.0;
}
static int cmpdouble(const void *aptr, const void *bptr)
{
const double a = *(const double *)aptr;
const double b = *(const double *)bptr;
if (a < b)
return -1;
else
if (a > b)
return +1;
else
return 0;
}
void report(double *const wall, double *const cpu, const size_t count)
{
printf("\tInitial call: %.0f cpu cycles, %.9f seconds real time\n", cpu[0], wall[0]);
qsort(wall, count, sizeof (double), cmpdouble);
qsort(cpu, count, sizeof (double), cmpdouble);
printf("\tMinimum: %.0f cpu cycles, %.9f seconds real time\n", cpu[0], wall[0]);
printf("\t5%% less than %.0f cpu cycles, %.9f seconds real time\n", cpu[count/20], wall[count/20]);
printf("\t25%% less than %.0f cpu cycles, %.9f seconds real time\n", cpu[count/4], wall[count/4]);
printf("\tMedian: %.0f cpu cycles, %.9f seconds real time\n", cpu[count/2], wall[count/2]);
printf("\t75%% less than %.0f cpu cycles, %.9f seconds real time\n", cpu[count-count/4-1], wall[count-count/4-1]);
printf("\t95%% less than %.0f cpu cycles, %.9f seconds real time\n", cpu[count-count/20-1], wall[count-count/20-1]);
printf("\tMaximum: %.0f cpu cycles, %.9f seconds real time\n", cpu[count-1], wall[count-1]);
}
int main(int argc, char *argv[])
{
struct timing measurement;
double *wall_seconds = NULL;
double *cpu_cycles = NULL;
unsigned long count = 0UL;
unsigned long i;
int flag;
char dummy;
if (argc != 3 || !strcmp(argv[1], "-h") || !strcmp(argv[1], "--help")) {
fprintf(stderr, "\n");
fprintf(stderr, "Usage: %s COUNT FLAG\n", argv[0]);
fprintf(stderr, "\n");
return 1;
}
if (sscanf(argv[1], " %lu %c", &count, &dummy) != 1) {
fprintf(stderr, "%s: Invalid COUNT.\n", argv[1]);
return 1;
}
if (count < 1UL) {
fprintf(stderr, "%s: COUNT is too small.\n", argv[1]);
return 1;
}
if (!(unsigned long)(count + 1UL)) {
fprintf(stderr, "%s: COUNT is too large.\n", argv[1]);
return 1;
}
if (sscanf(argv[2], " %d %c", &flag, &dummy) != 1) {
fprintf(stderr, "%s: Invalid FLAG.\n", argv[2]);
return 1;
}
wall_seconds = malloc(sizeof (double) * (size_t)count);
cpu_cycles = malloc(sizeof (double) * (size_t)count);
if (!wall_seconds || !cpu_cycles) {
free(cpu_cycles);
free(wall_seconds);
fprintf(stderr, "Cannot allocate enough memory. Try smaller COUNT.\n");
return 1;
}
printf("Call and measurement overhead:\n");
fflush(stdout);
for (i = 0UL; i < count; i++) {
start_timing(&measurement);
test_none(i, flag);
stop_timing(&measurement);
wall_seconds[i] = wall_timing(&measurement);
cpu_cycles[i] = cpu_timing(&measurement);
}
report(wall_seconds, cpu_cycles, (size_t)count);
printf("\n");
printf("Measuring FLAG==0 calls: ");
fflush(stdout);
test_calls = 0L;
test_sum = 0L;
for (i = 0UL; i < count; i++) {
start_timing(&measurement);
test_work(i, 0);
stop_timing(&measurement);
wall_seconds[i] = wall_timing(&measurement);
cpu_cycles[i] = cpu_timing(&measurement);
}
printf("%ld calls, sum %ld.\n", test_calls, test_sum);
report(wall_seconds, cpu_cycles, (size_t)count);
printf("\n");
printf("Measuring FLAG==%d calls:", flag);
fflush(stdout);
test_calls = 0L;
test_sum = 0L;
for (i = 0UL; i < count; i++) {
start_timing(&measurement);
test_work(i, flag);
stop_timing(&measurement);
wall_seconds[i] = wall_timing(&measurement);
cpu_cycles[i] = cpu_timing(&measurement);
}
printf("%ld calls, sum %ld.\n", test_calls, test_sum);
report(wall_seconds, cpu_cycles, (size_t)count);
printf("\n");
printf("Measuring alternating FLAG calls: ");
fflush(stdout);
test_calls = 0L;
test_sum = 0L;
for (i = 0UL; i < count; i++) {
start_timing(&measurement);
test_work(i, i & 1);
stop_timing(&measurement);
wall_seconds[i] = wall_timing(&measurement);
cpu_cycles[i] = cpu_timing(&measurement);
}
printf("%ld calls, sum %ld.\n", test_calls, test_sum);
report(wall_seconds, cpu_cycles, (size_t)count);
printf("\n");
free(cpu_cycles);
free(wall_seconds);
return 0;
}
将这三个文件放在一个空目录中,然后编译并构建./call-test
:
rm -f *.o
gcc -W -Wall -O3 -fomit-frame-pointer -c harness.c
gcc -W -Wall -O3 -fomit-frame-pointer -c work.c
gcc -W -Wall -O3 -fomit-frame-pointer -c test.c
gcc harness.o work.o test.o -lrt -o call-test
在AMD Athlon II X4 640上,使用gcc-4.6.3(Xubuntu 10.04),正在运行
./call-test 1000000 1
告诉我,当调用增加{{1}的第二个函数时,开销仅为2个时钟周期(<1ns)进行单独测试(分支未采用),仅4个时钟周期(仅超过1纳秒)并将计数器添加到test_calls
。
当省略所有优化时(使用test_sum
并在编译时省略-O0
),单独测试花费大约3个时钟周期(如果不采用分支则需要3个周期),如果分支则需要大约9个周期采取并完成工作以更新两个额外的变量。
(这两个额外的变量可以让你轻松看到线束确实做了它应该做的所有事情;它们只是一个额外的检查。我想在第二个函数中有一些工作,所以时间差异会更容易发现。)
以上解释仅适用于已缓存代码的情况;即最近运行。如果代码很少运行,它将不在缓存中。但是,测试开销更重要。缓存效果 - 例如,如果已经运行了“附近”代码(你可以看到这个用于调用开销测量,其他测试函数代码'也倾向于缓存!) - 无论如何都要大得多。 (虽然测试工具确实单独产生初始调用结果,但不要过于相信它,因为它不会尝试以任何方式清除任何缓存。)
我的结论是添加
-fomit-frame-pointer
任何正常的代码都很好:开销几乎可以忽略不计;几乎无关紧要。与往常一样,请考虑整体算法。与担心编译器生成的代码相比,算法中的任何增强都会产生更大的回报。
(由于我上面一次编写测试代码,其中可能存在一些错误和/或脑筋。检查,如果你发现任何问题,请在下面告诉我,以便我可以修复代码。)