拒绝switch,程序加速之函数指针数组
先看一个使用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非常多时,使用函数指针数组要快非常多。
拒绝switch,程序加速之函数指针数组的更多相关文章
- C 函数指针数组
名字有点绕口,其实更应该翻译为指针函数数组. 记录下对Head-First C这一节的理解,几乎每天班车上都会咪两眼,几乎每次都是看不懂,敲一敲的时候才有些明白. 通俗点讲,这功能解决的是,具有同种签 ...
- C++ code:函数指针数组
函数指针作为一种数据类型,当然可以作为数组的元素类型.例如,要实现用菜单来驱动函数调用的程序框架,则用函数指针数组来实现就比较容易维护. #include<iostream> using ...
- 转:函数指针数组的妙用(I)
转自:http://blog.sina.com.cn/s/blog_4c78b35f010008hi.html 笔者在开发某软件过程中遇到这样一个问题,前级模块传给我二进制数据,输入参数为 char* ...
- C 函数指针 函数指针数组 转移表
内容来自<c和指针>,整理后方便个人理解 高级声明 cdel程序可以方便的给出声明的释义 指向函数的指针 int ( *f ) ( int n_values, float amount ) ...
- C++基础——函数指针 函数指针数组
==================================声明================================== 本文版权归作者所有. 本文原创,转载必须在正文中显要地注明 ...
- typedef 函数指针 数组 std::function
1.整型指针 typedef int* PINT;或typedef int *PINT; 2.结构体 typedef struct { double data;}DATA, *PDATA; //D ...
- C#委托与C语言函数指针及函数指针数组
C#委托与C语言函数指针及函数指针数组 在使用C#时总会为委托而感到疑惑,但现在总新温习了一遍C语言后,才真正理解的委托. 其实委托就类似于C/C++里的函数指针,在函数传参时传递的是函数指针,在调用 ...
- C/C++ 一段代码区分数组指针|指针数组|函数指针|函数指针数组
#include<stdio.h> #include<stdlib.h> #include<windows.h> /* 举列子说明什么是函数指针 */ //以一个加 ...
- C/C++ 不带参数的回调函数 与 带参数的回调函数 函数指针数组 例子
先来不带参数的回调函数例子 #include <iostream> #include <windows.h> void printFunc() { std::cout<& ...
随机推荐
- selenium3 + python3 - alert定位
一.alert\confirm\prompt弹出框操作主要方法有: text:获取文本值 accept() :点击"确认" dismiss() :点击"取消"或 ...
- CALayer(一)
CALayer CALayer和UIView CALayer和UIView相比--CALayer少了事件处理的功能,所以更加轻量级,性能更好一点,这就说明如果有一些和用户交互的东西是不建议用CALay ...
- js基础---数组方法
数组数据的排序及去重 sort无形参的排序方式 arr1=[2,12,3,15]; var a=arr1.sort();console.log(arr1);console.log(a);//排序会改变 ...
- java编程基础篇---------> 编写一个程序,从键盘输入三个整数,求三个整数中的最小值。
编写一个程序,从键盘输入三个整数,求三个整数中的最小值. 关键:声明变量temp 与各数值比较. package Exam01; import java.util.Scanner; public ...
- mysql主从不同步,提示更新找不到记录
查看丛库状态show slave status\G 从库原文提示:Last_Error: Coordinator stopped because there were error(s) in the ...
- boost::mutex::scoped_lock
在三维重建过程中,世界地图 Map &world作为唯一 访问/更新 对象,可以使用boost::mutex::scoped_lock . 一:boost::mutex::scoped_loc ...
- msmq消息队列使用场景
MSMQ全称是Microsoft Message Queue——微软消息队列. MSMQ是一种通信的机制,因为是一种中间件技术,所以它能够支持多种类型的语言开发,同时也是跨平台的通信机制,也就是说MQ ...
- Python-通过configparser读写配置文件
Python读写配置文件: 1.创建配置文件(文件名以.conf或.ini结束的文件表示配置文件) 2.导入所需模块 OS, configparser >>> import os & ...
- Shell 在手分析服务器日志不愁
转自:https://wujunze.com/server_logs_analysis.jsp 自己的小网站跑在阿里云的ECS上面,偶尔也去分析分析自己网站服务器日志,看看网站的访问量.看看有没有骇客 ...
- php第八节课
加载 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.o ...