拒绝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<& ...
随机推荐
- 网上干货 ElasticSearch详解与优化设计
https://blog.csdn.net/joez/article/details/52171199?locationNum=3&fps=1 分析得很好,需要仔细阅读 问题遗留点: Fiel ...
- Android Fragment间的广播消息接收
这种方式不用在配置文件加东西,我比较喜欢. 广播注册,可以写在Activity(onCreate),也可以写在Fragment(onActivityCreated)里. LocalBroadcastM ...
- ThreadPoolExecutor理解
ThreadPoolExecutor组成 ThreadPoolExecutor的核心构造函数: public ThreadPoolExecutor(int corePoolSize, int maxi ...
- 基于神经网络的混合计算(DNC)-Hybrid computing using a NN with dynamic external memory
前言: DNC可以称为NTM的进一步发展,希望先看看这篇译文,关于NTM的译文:人工机器-NTM-Neutral Turing Machine 基于神经网络的混合计算 Hybrid computing ...
- 设计包含min()函数的栈
题目:定义栈的数据结构,要求添加一个min函数,能够得到栈的最小元素.要求函数min.push以及pop的时间复杂度都是O(1). 分析:这是去年google的一道面试题. 我看到这道题目时,第一反应 ...
- css3基础篇二
CSS3 边框 border-radius box-shadow border-image(ie不支持) 语法 border-radius: 1-4 length|% / 1-4 length|%; ...
- APICloud 获取缓存以及清除缓存(常用第三方方法)
一.app中经常会有缓存的清除这个操作,具体如下 1.获取缓存大小 apiready = function() { api.getCacheSize(function(ret, err) { //si ...
- python时间序列按频率生成日期
有时候我们的数据是按某个频率收集的,比如每日.每月.每15分钟,那么我们怎么产生对应频率的索引呢?pandas中的date_range可用于生成指定长度的DatetimeIndex.我们先看一下怎么生 ...
- 【转载】jmeter将上一个接口返回值作为下一个接口的请求参数
第一:通过JSON Extractor 插件来提取JSON响应结果 原文地址:http://blog.csdn.net/dreamtl/article/details/68957122 接口响应结果, ...
- PAT_A1078#Hashing
Source: PAT A1078 Hashing (25 分) Description: The task of this problem is simple: insert a sequence ...