weixin_39601511
2020-12-31 12:37 阅读 2

fprintf function is too slow when save something to file

In iniparser_dump_ini() and iniparser_dumpsection_ini(), the author uses fprintf to save ini data to file. When I use those functions ,I found it took me 7~8 seconds every time to save all info to a file. My embedded platform is MIPS and OS is Nuttx. After a long period of debugging, I used sprintfand fwriteto replace fprintf. Then it only took me 1 second to save completely. So I feel that I have the duty to report this .

在函数 iniparser_dump_ini() 和 iniparser_dumpsection_ini() 中,作者使用fprintf函数来保存数据到文件。当我用libiniparse库的时候,我发现每次将数据保存到文件都需要5~6秒。我的平台是MIPS,系统是Nuttx。经过一段时间的调试,我用sprintf来格式化数据,然后用fwrite将数据写入文件,这种方式只需要不到1秒钟的时间。我感觉我有责任来报告一下这个问题。

该提问来源于开源项目:ndevilla/iniparser

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

15条回答 默认 最新

  • weixin_39890543 weixin_39890543 2020-12-31 12:37

    你好,中国聪明青蛙 ;-)

    Thank for the report, it is possible you platform is forcing buffer flush when using fprintf (for example each time it encounter a \n, which should not be the case given normally file are block buffered).

    I guess you should be able to use setbuf to change the buffer behavior of you file (this could even improve the performances of you current fix by setting a bigger buffer)

    下个错误见 !

    点赞 评论 复制链接分享
  • weixin_39624071 weixin_39624071 2020-12-31 12:37

    If we add setvbuf(f, NULL, _IOFBF, 0) before writing key-value to file, we can resolve this problem. SmartFrogFromChina is not the only person who met this problem~ I have checked glibc source code, setvbuf() function should be fully supported.

    点赞 评论 复制链接分享
  • weixin_39890543 weixin_39890543 2020-12-31 12:37

    Thanks for the feedback

    do you thing this is worth mentioning in the faq ?

    点赞 评论 复制链接分享
  • weixin_39624071 weixin_39624071 2020-12-31 12:37

    I do not think setvuf() or setvbuf is usual API. I have meet performance degradation causing by '\n' for several times. Orz If possible, calling setvbuf by libiniparser is a simple and effective way~

    点赞 评论 复制链接分享
  • weixin_39621695 weixin_39621695 2020-12-31 12:37

    I would avoid calling setvbuf() from iniparser itself, rather recommend the caller does it before writing to a file. Seems a bit platform-specific to me, but certainly worth mentioning in the FAQ. Let me add an FAQ here where this kind of thing could be detailed and documented. There are certainly other platforms with buffering issues and such.

    点赞 评论 复制链接分享
  • weixin_39890543 weixin_39890543 2020-12-31 12:37

    Let me add an FAQ here

    remember we have a faq section in the readme then :smile:

    点赞 评论 复制链接分享
  • weixin_39621695 weixin_39621695 2020-12-31 12:37

    I know :-) Maybe better to have a separate file? I let you decide.

    点赞 评论 复制链接分享
  • weixin_39890543 weixin_39890543 2020-12-31 12:37

    I think so far the number of questions are small enough to be kept on the README (gave them more visibility this way)

    点赞 评论 复制链接分享
  • weixin_39624071 weixin_39624071 2020-12-31 12:37

    I'd like to translate the FAQ to Chinese. It seems like that a lot of Chinese developer like libiniparser~ Could I make a pull request ?

    点赞 评论 复制链接分享
  • weixin_39890543 weixin_39890543 2020-12-31 12:37

    行!汉语翻译是一个非常好的思想!^^

    点赞 评论 复制链接分享
  • weixin_39621695 weixin_39621695 2020-12-31 12:37

    Beautiful idea! Worth detaching the FAQ to a separate file to ease translation, then. Let me commit a first draft by copying the relevant parts from README.

    点赞 评论 复制链接分享
  • weixin_39621695 weixin_39621695 2020-12-31 12:37

    added FAQ-en.md and modified README to add a pointer. Let me know if this works for you. A MILLION THANKS FOR THIS!

    点赞 评论 复制链接分享
  • weixin_39890543 weixin_39890543 2020-12-31 12:37

    I've added a url link on the faq in the README, otherwise it's good for me

    点赞 评论 复制链接分享
  • weixin_39624071 weixin_39624071 2020-12-31 12:37

    I had made a pull-request #87 . Should I do anything else?

    点赞 评论 复制链接分享
  • weixin_39890543 weixin_39890543 2020-12-31 12:37

    you should allow yourself a beer for the great work ;-)

    点赞 评论 复制链接分享

相关推荐