Tag: 跳转表

“开关”比“如果”更快?

switch语句实际上比if语句快吗? 我使用/Ox标志在Visual Studio 2010的x64 C ++编译器上运行以下代码: #include <stdlib.h> #include <stdio.h> #include <time.h> #define MAX_COUNT (1 << 29) size_t counter = 0; size_t testSwitch() { clock_t start = clock(); size_t i; for (i = 0; i < MAX_COUNT; i++) { switch (counter % 4 + 1) { case 1: counter += 4; break; case 2: counter […]