免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
12下一页
最近访问板块 发新帖
查看: 10454 | 回复: 18
打印 上一主题 下一主题

寻找比WritePrivateProfileString更快的读写INI文件的方法 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2008-08-25 23:28 |只看该作者 |倒序浏览
如题,
找了好久都没找到:em11: :em11:

最好是C代码

测试结果
P4 2.4  1G
windows API WritePrivateProfileString
1000次  4.263224   秒
5000      42.993980 秒
大家可以测试一下,现在基本上没找到比windows API WritePrivateProfileString  更快的写INI的方法

附上测试代码



int main()
{
    const char *file ="c:\\myconfig.ini";
    const char *section = "student";
    const char *name_key = "name";
    const char *age_key = "age";
    char name[BUF_SIZE]={0};
    int i;
    char buf[10];

    tstart();
    for (i=0;i<5000;i++) {

         WritePrivateProfileString(itoa(i, buf, 10), name_key, "123411111111111111111111111111111111111111111", file);
         WritePrivateProfileString(itoa(i, buf, 10), "12398774", "123411111111111111111111111111111111111111111", "c:\\myconfig1.ini");
//         if(!GetPrivateProfileString(section, name_key, "123", name, BUF_SIZE,file)) {

//             printf("read ini file fail\n";

//             return -1;

//         }

    }
    tend();
    printf("%f\n", tval());
    return 0;
}


[ 本帖最后由 xfly_t 于 2008-8-26 13:02 编辑 ]

论坛徽章:
0
2 [报告]
发表于 2008-08-25 23:38 |只看该作者

回复 #1 xfly_t 的帖子

自己写一个函数,ini格式应该还是比较简单的,sprintf配合write是不是就可以解决了。

[ 本帖最后由 xiexiecn 于 2008-8-25 23:40 编辑 ]

论坛徽章:
0
3 [报告]
发表于 2008-08-25 23:49 |只看该作者
据说iniParser可以

论坛徽章:
0
4 [报告]
发表于 2008-08-25 23:52 |只看该作者

论坛徽章:
1
2015年迎新春徽章
日期:2015-03-04 09:49:45
5 [报告]
发表于 2008-08-25 23:55 |只看该作者
感觉用flex + bison写一个不会太难, 当然前提是你得会这两个工具。

论坛徽章:
0
6 [报告]
发表于 2008-08-26 09:04 |只看该作者
大家都勿略了一个重要的问题,实现功能很容易但
要求更快,效率要更高

这不是那么容易,大家可以用我的那个测试例子看下

P4 2.4  1G
windows
1000次  4.263224   秒
5000      42.993980 秒

500次时自己写的,打开文件读取内容就要200多秒

[ 本帖最后由 xfly_t 于 2008-8-26 09:09 编辑 ]

论坛徽章:
4
2015年辞旧岁徽章
日期:2015-03-03 16:54:152015年迎新春徽章
日期:2015-03-04 09:56:11IT运维版块每日发帖之星
日期:2016-08-11 06:20:00IT运维版块每日发帖之星
日期:2016-08-15 06:20:00
7 [报告]
发表于 2008-08-26 10:23 |只看该作者

回复 #1 xfly_t 的帖子

可以试试FastDFS中的ini reader,完全用C编写,支持一个key出现多次的情况,查找key时采用折半查找法,性能应该很不错!
下载地址:http://code.google.com/p/fastdfs/downloads/list
V1.6下载链接:http://fastdfs.googlecode.com/files/FastDFS_v1.6.tar.gz

common目录下的ini_file_reader.h和ini_file_reader.c
调用示例:

     IniItemInfo *items;
        int nItemCount;
        int result;
        char *ppTrackerServers[FDFS_MAX_TRACKERS];
        int tracker_server_count;
        int max_connections;
        char *conf_filename;
        bool disabled;
        int i;
        
        conf_filename = "sample.ini";
        if ((result=iniLoadItems(conf_filename, &items, &nItemCount)) != 0)
        {
                printf("file: "__FILE__", line: %d, " \
                        "load from ini file \"%s\" fail, " \
                        "error code: %d", \
                        __LINE__, conf_filename, result);
                return result;
        }
        
        pGroupName = iniGetStrValue("group_name", items, nItemCount);
        if (pGroupName == NULL)
        {
             iniFreeItems(items);
             printf("file: "__FILE__", line: %d, " \
                    "conf file \"%s\" must have item " \
                    "\"group_name\"!", \
                    __LINE__, conf_filename);
             return ENOENT;
        }
        
        max_connections = iniGetIntValue("max_connections", \
                        items, nItemCount, 256);

        disabled = iniGetBoolValue("disabled", items, nItemCount);
      
        if ((tracker_server_count=iniGetValues("tracker_server", \
                items, nItemCount, ppTrackerServers, \
                FDFS_MAX_TRACKERS)) <= 0)
        {
            iniFreeItems(items);
            printf("file: "__FILE__", line: %d, " \
                    "conf file \"%s\", get item \"tracker_server\" fail", \
                    __LINE__, filename);
            return ENOENT;
        }
        
        for (i=0; i<tracker_server_count; i++)
        {
            printf("%d. %s\n", i+1, ppTrackerServers);
        }
        
        iniFreeItems(items);
        return 0;

论坛徽章:
0
8 [报告]
发表于 2008-08-26 10:41 |只看该作者
弱弱的问下,ini文件需要去频繁读写吗?

论坛徽章:
0
9 [报告]
发表于 2008-08-26 11:57 |只看该作者

回复 #8 syshunter 的帖子

用INI保存一些数据,在测试中用

论坛徽章:
0
10 [报告]
发表于 2008-08-26 12:26 |只看该作者

回复 #9 xfly_t 的帖子

呃...这种需求还真没见过...
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP