先看一个使用switch语句的程序:

#include <stdio.h>
#include <time.h> //加法
int add(int a,int b)
{
return a+b;
} //减法
int subtract(int a,int b)
{
return a-b;
} //乘法
int multi(int a,int b)
{
return a*b;
} //除法
int divide(int a,int b)
{
return a/b;
} int claculate(int a,int b,char oper)
{
//这里使用switch语句
switch (oper)
{
case '+':
return add(a,b);
case '-':
return subtract(a,b);
case '*':
return multi(a,b);
case '/':
return divide(a,b);
default:
return -1;
break;
}
} void main()
{
int a = 250;
int b = 5;
//统计程序执行时间
clock_t start, finish;
start = clock();
printf("%d\n",claculate(a,b,'+'));
printf("%d\n",claculate(a,b,'-'));
printf("%d\n",claculate(a,b,'*'));
printf("%d\n",claculate(a,b,'/'));
finish = clock();
printf( "%f seconds\n", (double)(finish - start) / CLOCKS_PER_SEC );
}

再看一个使用函数指针数组的程序

#include <stdio.h>
#include <time.h> //加法
int add(int a,int b)
{
return a+b;
} //减法
int subtract(int a,int b)
{
return a-b;
} //乘法
int multi(int a,int b)
{
return a*b;
} //除法
int divide(int a,int b)
{
return a/b;
} int claculate(int a,int b,int oper)
{
//事实上这里应该使用hashMap。将字符'+'映射到add函数。 //直接使用数字是为了简便 //声明指向函数指针的数组
int (*pfunc[])(int a,int b) = {add,subtract,multi,divide}; return pfunc[oper](a,b);
} void main()
{
int a = 250;
int b = 5;
//统计程序执行时间
clock_t start, finish;
start = clock();
printf("%d\n",claculate(a,b,0));
printf("%d\n",claculate(a,b,1));
printf("%d\n",claculate(a,b,2));
printf("%d\n",claculate(a,b,3));
finish = clock();
printf( "%f seconds\n", (double)(finish - start) / CLOCKS_PER_SEC );
}

当switch推断语句中case的个数不多时,上面两个程序几乎相同。但假设case非常多时,使用函数指针数组要快非常多。

类似地。在Java里面也能够使用反射来代替swith语句产生类似的效果。



拒绝switch,程序加速之函数指针数组的更多相关文章

  1. C 函数指针数组

    名字有点绕口,其实更应该翻译为指针函数数组. 记录下对Head-First C这一节的理解,几乎每天班车上都会咪两眼,几乎每次都是看不懂,敲一敲的时候才有些明白. 通俗点讲,这功能解决的是,具有同种签 ...

  2. C++ code:函数指针数组

    函数指针作为一种数据类型,当然可以作为数组的元素类型.例如,要实现用菜单来驱动函数调用的程序框架,则用函数指针数组来实现就比较容易维护. #include<iostream> using ...

  3. 转:函数指针数组的妙用(I)

    转自:http://blog.sina.com.cn/s/blog_4c78b35f010008hi.html 笔者在开发某软件过程中遇到这样一个问题,前级模块传给我二进制数据,输入参数为 char* ...

  4. C 函数指针 函数指针数组 转移表

    内容来自<c和指针>,整理后方便个人理解 高级声明 cdel程序可以方便的给出声明的释义 指向函数的指针 int ( *f ) ( int n_values, float amount ) ...

  5. C++基础——函数指针 函数指针数组

    ==================================声明================================== 本文版权归作者所有. 本文原创,转载必须在正文中显要地注明 ...

  6. typedef 函数指针 数组 std::function

    1.整型指针 typedef int* PINT;或typedef int *PINT; 2.结构体 typedef struct { double data;}DATA,  *PDATA;  //D ...

  7. C#委托与C语言函数指针及函数指针数组

    C#委托与C语言函数指针及函数指针数组 在使用C#时总会为委托而感到疑惑,但现在总新温习了一遍C语言后,才真正理解的委托. 其实委托就类似于C/C++里的函数指针,在函数传参时传递的是函数指针,在调用 ...

  8. C/C++ 一段代码区分数组指针|指针数组|函数指针|函数指针数组

    #include<stdio.h> #include<stdlib.h> #include<windows.h> /* 举列子说明什么是函数指针 */ //以一个加 ...

  9. C/C++ 不带参数的回调函数 与 带参数的回调函数 函数指针数组 例子

    先来不带参数的回调函数例子 #include <iostream> #include <windows.h> void printFunc() { std::cout<& ...

随机推荐

  1. 浅谈自学Python之路(购物车程序练习)

    购物车程序练习 今天我们来做一个购物车的程序联系,首先要理清思路 购物车程序需要用到什么知识点 需要用到哪些循环 程序编写过程中考虑值的类型,是int型还是字符串 如果值为字符串该怎么转成int型 用 ...

  2. go 条件语句if

    一.if 语句 格式 if condition { // do something } 举例 package main import "fmt" func main(){ var ...

  3. 【NOI1999、LOJ#10019】生日蛋糕(搜索、最优化剪枝、可行性剪枝)

    主要是剪枝的问题,见代码,讲的很详细 #include<iostream> #include<cstdio> #include<cmath> #include< ...

  4. elasticsearch性能调优

    转载 http://www.cnblogs.com/hseagle/p/6015245.html 该es调优版本可能有低,但是思想主体不变,不合适的参数可以自己找最新的版本相应的替代,或者增删 ela ...

  5. 5.23@Comfiguration的解释

    @Configuration:代表这个类是一个配置类. @ComponentScan:用来扫描指定包下面的注解类. @Import:用来导入其他的@Configuration配置类. @ImportR ...

  6. Quartz 表达式的学习

    只记录用到过的,不全面 Quartz版本:quartz-all-1.6.0.jar 先看图 其他示例: 0 5,6,13 * * ? 意义:每日5:00,6:00,13:00 被触发 0 10,30 ...

  7. scrollWidth clientWidth offsetWidth

    scrollWidth:对象的实际内容的宽度,不包边线宽度,会随对象中内容超过可视区后而变大.         实际内容+padding                 不包括滚动条 边框client ...

  8. P1569 [USACO11FEB]属牛的抗议Generic Cow Prote…

    题目描述 Farmer John's N (1 <= N <= 100,000) cows are lined up in a row and numbered 1..N. The cow ...

  9. (转)vuex2.0 基本使用(2) --- mutation 和 action

    我们的项目非常简单,当点击+1按钮的时候,count 加1,点击-1按钮的时候,count 减1. 1, mutation The only way to actually change state ...

  10. ★Java语法(五)——————————三元运算符

    package 课上练习; public class 三元运算符 { //用法: 数据类型 变量 = 布尔表达式? 条件满足设置内容:条件不满足设置内容 : public static void ma ...