透明C++数字/整数包装器
我已经创建了一个处理 unsigned int(8, 16, 32...) 位的类,并且我一直在尝试将其与裸按位运算进行基准测试,但对按位运算的速度进行了准确的测试至少可以说很复杂。使用包装器的原因是它使用起来要简单得多。
现在,虽然这在学术上可能比实际更有用,但我想知道是否可以创建一个围绕 int 的类,该类非常透明,就像直接使用 int (使用所有可能的运算符)一样,除了使用函数让我可以以某种方式操作它,并且可以尽可能快(有很多内联)。
我可以使用任何和所有 C++0x 功能来实现此目的。
I've made a class for working with bits for unsigned int(8, 16, 32...), and I've been trying to benchmark it compared to the bare bitwise operations but getting an accurate test of the speed of bitwise operations is complicated to say the least. The reason for the wrapper is that it is a lot less complicated to use.
Now, while this may be more academically useful than practically, I would like to know if it was possible to make a class wrapping around an int that is so transparent that it is like working with the int directly (with all possible operators) except with functions that let me manipulate it in certain ways, and if it is possible to be as fast (with a lot of inlining).
I'm okay with using any and all C++0x features to achieve this.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以设置自定义类并重载所有相关运算符(算术运算符、比较运算符、按位运算符、转换运算符),并根据需要提供您自己的行为。
据我所知,确实可以使其在几乎所有方面都表现得像
int
。内联编译器可能会在消除大部分开销方面做得非常好,但要了解结果有多好,您需要尝试一下。
不过,这种类型对于基准测试几乎没有什么用处 - 我宁愿重复相同的操作,直到测量中的相对误差变小。或多或少不可能对单个按位运算之类的速度进行计时,因为它实际上归结为一两个机器指令。
You can setup a custom class and overload all the relevant operators (arithmetic operators, comparison operators, bitwise operators, conversion operators) and supply your own behaviour as needed.
As far as I know, it would indeed be possible to make it behave like an
int
in almost all respects.An inlining compiler will presumably do a very good job at removing most of the overhead, but to find out how good the results are, you will need to try it out.
Such a type would have little use for benchmarking, though - I'd rather repeat the very same operations until the relative error in your measurements get smaller. It's more or less impossible to time the speed of a something like a single bitwise operation as it really boils down to one or two machine instructions.
模板?
另外,分析您的运营情况有何困难?通常的做法是做同样的操作X次,然后取平均值。如果您使用的是 Intel CPU,您可以在每次操作之前和之后读取 CPU 的计时器,然后获取平均值,它有一些缺陷,但对于您的目的来说应该相对没问题
http://en.wikipedia.org/wiki/Time_Stamp_Counter
另外,您可以查看编译器在使用您的类时生成的 asm,看看是否有您可以重构代码的任何领域,以便它可以更好地进行优化。
Templates?
Also, what is the difficulty in profiling your operations? The usual way is to do the same operation X times and then get the average. If you're on an Intel CPU you can read the CPU's timers before and after each operation and then get the average it has some flaws but should be relatively ok for your purposes
http://en.wikipedia.org/wiki/Time_Stamp_Counter
Also, you can look at the asm the compiler generates when your class is used and see if there are any areas where you can refactor your code so that it can do a better job at optimisations.
是的,这是可能的;但我认为从长远来看,您可能会发现编写一些模板化内联函数会更容易,您可以使用各种 int8/int16/etc 参数来调用这些函数。这样,您就不必担心如何处理将类的对象传递给需要常规整数的函数,反之亦然。 (并不是说这也不可行,但它可能会让事情变得更简单,完全避免这个问题)
Yes, it's possible; but I think you might find it easier in the long run to just write some templated inline functions that you can call with various int8/int16/etc arguments. That way you don't have to worry about how to handle passing objects of your class to functions that expect regular integers, and vice versa. (Not that that isn't doable as well, but it might keep things simpler to avoid that issue entirely)