的&QUOT国; memset的"在C ++与现代编译器功能 [英] State of "memset" functionality in C++ with modern compilers

查看:141
本文介绍了的&QUOT国; memset的"在C ++与现代编译器功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

上下文:

前一段时间,我无意中发现这2001 DDJ Alexandrescu的文章在:
http://www.ddj.com/cpp/184403799

这是关于比较各种方法来初始化一个缓冲区,以一定的价值。像什么memset的做单字节的值。他比较各种实现(memcpy的,明确的for循环,达夫的设备),并没有真正找到所有数据集的大小和所有的编译器的最佳人选。

报价:


  

有一个非常深刻的,和悲伤,基本实现这一切。我们是在2001年,空间奥德赛的一年。 (...)只要走出的方块,看看我们 - 50年后,我们仍然没有填充和拷贝内存非常好


问:


  1. 有没有人有此问题的更多最新信息?最近做GCC和Visual C ++实现的性能比7年前显著更好?
  2. 我在写code具有5+(大概10 +)年的寿命,并且将处理数组的大小从几个字节到兆百元。我不能假设我现在的选择仍然是最佳的5年。我该怎么办:

         
    • A)使用系统的memset的(或同等学历),而忘记了最佳性能,或承担运行和编译器将处理这对我来说。
         
    • 二)基准一劳永逸几个程序之间的各种数组的大小和编译器及开关在运行时。
         
    • C)运行在程序初始化的基准,并基于准确的(?)的数据在运行时切换。

编辑:我工作的图像处理软件。我的阵列项目吊舱和每毫秒数!

编辑2:谢谢你的答案首先,这里有一些额外的信息:

  • 缓冲区初始化可能重新present 20%的一些算法总运行时间-40%。
  • 该平台可以在未来的5年以上变化,尽管它会留在类别最快的CPU金钱可以买到戴尔。编译器将是某种形式的GCC和Visual C ++的。雷达
  • 没有内嵌的东西或外来的架构,我想从人谁必须更新他们的软件时,MMX和SSE出现了,因为我必须做同样的当SSE2015变得可以听到... :)


解决方案

在DDJ文章承认,memset的是最好的答案,而不是他试图实现更快:


  

也有一些是神圣不可侵犯
  C'S存储器操作功能
  memset的,memcpy的和memcmp。他们是
  可能被高度优化
  编译器供应商的范围内,
  编译器可能会检测到电话
  这些功能与替换它们
  内联汇编指令 - 这
  是MSVC的情况。


所以,如果memset的为你工作(即您使用的是单字节初始化),然后使用它。

虽然每毫秒可以指望,你应该建立什么样的百分比的执行时间是输给设置内存。这是因为你有一些有用的工作要做,以及可能非常低(1%或2%??)。鉴于优化的努力可能会在别处的回报更好的速度。

Context:

A while ago, I stumbled upon this 2001 DDJ article by Alexandrescu: http://www.ddj.com/cpp/184403799

It's about comparing various ways to initialized a buffer to some value. Like what "memset" does for single-byte values. He compared various implementations (memcpy, explicit "for" loop, duff's device) and did not really find the best candidate across all dataset sizes and all compilers.

Quote:

There is a very deep, and sad, realization underlying all this. We are in 2001, the year of the Spatial Odyssey. (...) Just step out of the box and look at us — after 50 years, we're still not terribly good at filling and copying memory.

Question:

  1. does anyone have more recent information about this problem ? Do recent GCC and Visual C++ implementations perform significantly better than 7 years ago ?
  2. I'm writing code that has a lifetime of 5+ (probably 10+) years and that will process arrays' sizes from a few bytes to hundred of megabytes. I can't assume that my choices now will still be optimal in 5 years. What should I do:
    • a) use the system's memset (or equivalent) and forget about optimal performance or assume the runtime and compiler will handle this for me.
    • b) benchmark once and for all on various array sizes and compilers and switch at runtime between several routines.
    • c) run the benchmark at program initialization and switch at runtime based on accurate (?) data.

Edit: I'm working on image processing software. My array items are PODs and every millisecond counts !

Edit 2: Thanks for the first answers, here are some additional informations:

  • Buffer initialization may represent 20%-40% of total runtime of some algorithms.
  • The platform may vary in the next 5+ years, although it will stay in the "fastest CPU money can buy from DELL" category. Compilers will be some form of GCC and Visual C++. No embedded stuff or exotic architectures on the radar
  • I'd like to hear from people who had to update their software when MMX and SSE appeared, since I'll have to do the same when "SSE2015" becomes available... :)

解决方案

The DDJ article acknowledges that memset is the best answer, and much faster than what he was trying to achieve:

There is something sacrosanct about C's memory manipulation functions memset, memcpy, and memcmp. They are likely to be highly optimized by the compiler vendor, to the extent that the compiler might detect calls to these functions and replace them with inline assembler instructions — this is the case with MSVC.

So, if memset works for you (ie. you are initializing with a single byte) then use it.

Whilst every millisecond may count, you should establish what percentage of your execution time is lost to setting memory. It is likely very low (1 or 2%??) given that you have useful work to do as well. Given that the optimization effort would likely have a much better rate of return elsewhere.

这篇关于的&QUOT国; memset的"在C ++与现代编译器功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆