jump table

来源:互联网 发布:如何在淘宝购物省钱 编辑:程序博客网 时间:2024/04/29 06:56

有效消除分支预测

#include <stdio.h>#include <stdlib.h> typedef void (*Handler)(void);    /* A pointer to a handler function */ /* The functions */void func3 (void) { printf( "3\n" ); }void func2 (void) { printf( "2\n" ); }void func1 (void) { printf( "1\n" ); }void func0 (void) { printf( "0\n" ); } Handler jump_table[4] = {func0, func1, func2, func3}; int main (int argc, char **argv) {    int value;     /* Convert first argument to 0-3 integer (Hash) */    value = atoi(argv[1]) % 4;    if (value < 0) {        value *= -1;    }     /* Call appropriate function (func0 thru func3) */    jump_table[value]();     return 0;}


原创粉丝点击