免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
楼主: yecheng_110
打印 上一主题 下一主题

多个strcmp比较的时候大家是怎么做的? [复制链接]

论坛徽章:
0
21 [报告]
发表于 2007-07-19 11:04 |只看该作者
表驱动.

论坛徽章:
0
22 [报告]
发表于 2007-07-19 11:13 |只看该作者
如果要频繁比较,可以用trie。概念很简单,自己实现也不难。

http://www.cs.bu.edu/teaching/c/tree/trie/
http://www.nist.gov/dads/HTML/trie.html

[ 本帖最后由 lgfang 于 2007-7-19 11:18 编辑 ]

论坛徽章:
0
23 [报告]
发表于 2007-07-19 11:18 |只看该作者
vafls_scott 说的我比较赞成。
几乎可以肯定楼主需要的是可读性和可维护性,而不是效率。

如果需要效率,可以分析一下运行需要多少时间,多少次,总和是多少,估计这个值会远远小于楼主所能接受值的%1

论坛徽章:
0
24 [报告]
发表于 2007-07-19 11:25 |只看该作者
写了一个例子
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(int argc,char *argv[])
{
        if (argc < 2)
                exit(1);
        int i = 0;
        while(i++ < 1000000)
        if (!strcmp(argv[1],"asdfasf"))
                printf("line: %d\n",__LINE__);
        else if (!strcmp(argv[1],"asfawfe"))
                printf("line: %d\n",__LINE__);
        else if (!strcmp(argv[1],"ssafwfwfj"))
                printf("line: %d\n",__LINE__);
        else if (!strcmp(argv[1],"asdfafwa "))
                printf("line: %d\n",__LINE__);
        else if (!strcmp(argv[1],"asdfwfewae"))
                printf("line: %d\n",__LINE__);
        else if (!strcmp(argv[1],"asfawfe"))
                printf("line: %d\n",__LINE__);
        else if (!strcmp(argv[1],"ssafwfwfj"))
                printf("line: %d\n",__LINE__);
        else if (!strcmp(argv[1],"asdfafwa "))
                printf("line: %d\n",__LINE__);
        else if (!strcmp(argv[1],"asdfwfewae"))
                printf("line: %d\n",__LINE__);
        else if (!strcmp(argv[1],"asfawfe"))
                printf("line: %d\n",__LINE__);
        else if (!strcmp(argv[1],"ssafwfwfj"))
                printf("line: %d\n",__LINE__);
        else if (!strcmp(argv[1],"asdfafwa "))
                printf("line: %d\n",__LINE__);
        else if (!strcmp(argv[1],"asdfwfewae"))
                printf("line: %d\n",__LINE__);
        else if (!strcmp(argv[1],"asfawfe"))
                printf("line: %d\n",__LINE__);
        else if (!strcmp(argv[1],"ssafwfwfj"))
                printf("line: %d\n",__LINE__);
        else if (!strcmp(argv[1],"asdfafwa "))
                printf("line: %d\n",__LINE__);
        else if (!strcmp(argv[1],"asdfwfewae"))
                printf("line: %d\n",__LINE__);
        else if (!strcmp(argv[1],"asfawfe"))
                printf("line: %d\n",__LINE__);
        else if (!strcmp(argv[1],"ssafwfwfj"))
                printf("line: %d\n",__LINE__);
        else if (!strcmp(argv[1],"asdfafwa "))
                printf("line: %d\n",__LINE__);
        else if (!strcmp(argv[1],"asdfwfewae"))
                printf("line: %d\n",__LINE__);
}



在我的机器上跑了一下

cc  ifeles.c
./a.out nosuchstring

real    0m0.941s
user    0m0.896s
sys     0m0.000s

cc  ifeles.c -O3
time ./a.out nosuchstring

real    0m0.235s
user    0m0.202s
sys     0m0.001s

论坛徽章:
95
程序设计版块每日发帖之星
日期:2015-09-05 06:20:00程序设计版块每日发帖之星
日期:2015-09-17 06:20:00程序设计版块每日发帖之星
日期:2015-09-18 06:20:002015亚冠之阿尔艾因
日期:2015-09-18 10:35:08月度论坛发贴之星
日期:2015-09-30 22:25:002015亚冠之阿尔沙巴布
日期:2015-10-03 08:57:39程序设计版块每日发帖之星
日期:2015-10-05 06:20:00每日论坛发贴之星
日期:2015-10-05 06:20:002015年亚冠纪念徽章
日期:2015-10-06 10:06:482015亚冠之塔什干棉农
日期:2015-10-19 19:43:35程序设计版块每日发帖之星
日期:2015-10-21 06:20:00每日论坛发贴之星
日期:2015-09-14 06:20:00
25 [报告]
发表于 2007-07-19 11:44 |只看该作者
原帖由 chmxu 于 2007-7-19 11:25 发表
写了一个例子
#include
#include
#include
int main(int argc,char *argv[])
{
        if (argc < 2)
                exit(1);
        int i = 0;
        while(i++ < 1000000)
        if (!strcmp(argv[1],"asdfasf"))
                printf("line ...

这种代码我看着就头疼。另外,实际中的代码往往比这个复杂(虽然结构一样),那就更令人头大了。

论坛徽章:
0
26 [报告]
发表于 2007-07-19 11:59 |只看该作者
是的,可读性不好,而且很难维护,最好是放到一个结构数组,然后遍历

论坛徽章:
0
27 [报告]
发表于 2007-07-19 12:08 |只看该作者
Do you wants to parse the command line args?
man getopt
man getoptlong

论坛徽章:
1
双子座
日期:2015-01-04 14:25:06
28 [报告]
发表于 2007-12-30 23:56 |只看该作者
原帖由 ivhb 于 2007-7-17 09:47 发表


一般来说,罗列(并且很多)这样的结构都可以通过仔细设计,适应表驱动法。
比如你可以
struct  {
  const char *name;
  int (*action)(void);
};

通过这个配置就可以把特征码和对应动作捆绑,如果 ...

以前那个程序做了就离职了
现在在做新的项目又遇到了这个问题
就是采用的这个办法了
过了几个月再来看以前的讨论 感觉自己进步了

[ 本帖最后由 yecheng_110 于 2007-12-31 00:01 编辑 ]

论坛徽章:
0
29 [报告]
发表于 2008-01-08 00:14 |只看该作者
create index 吧

论坛徽章:
0
30 [报告]
发表于 2008-01-08 09:17 |只看该作者
通用的方法
(不一定是最好的)

int memcmp(const void *s1, const void *s2, size_t n);

DESCRIPTION
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;The memcmp() function compares the first n bytes of the memory areas s1
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;and s2.  It returns an integer less than, equal  to,  or  greater  than
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;zero  if  s1  is  found, respectively, to be less than, to match, or be
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;greater than s2.
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP