显式初始化的效率
我有一个类,它有一个采用 const char*
的构造函数。它是:
c::c(const char* str) {
a = 32;
f = 0;
data = new char[strlen(str)];
memcpy(data, str, strlen(str));
}
以及一个采用其中之一的函数:
int foo(c& cinst);
您可以通过向其传递一个 c
实例来调用该函数:
c cinst("asdf");
foo(cinst);
或者,因为我们有显式初始化,您可以这样做:
foo("asdf");
这将使c
通过传递构造函数“asdf”,然后将结果对象传递给 foo
。
然而,这似乎比仅仅重载 foo 来获取 const char* 的效率要低得多。是否值得为了速度而进行过载,或者性能影响是否很小以至于进行过载会浪费空间?我试图让我的程序尽可能快,所以速度是一个重要因素,大小也是如此,但不是那么重要。
I have a class which has a constructor that takes a const char*
. It is:
c::c(const char* str) {
a = 32;
f = 0;
data = new char[strlen(str)];
memcpy(data, str, strlen(str));
}
And a function which takes one of them:
int foo(c& cinst);
You can call this function either by passing it an instance of a c
:
c cinst("asdf");
foo(cinst);
or, because we have explicit initialization, you can do:
foo("asdf");
which will make a c
by passing the constructor "asdf" and then pass the resulting object to foo
.
However, this seems like it might be quite a bit less efficient than just overloading foo
to take a const char*
. Is it worth doing the overload for the speed or is the performance impact so small that it's a waste of space to make an overload? I'm trying to make my program as fast as possible, so speed is an important factor, so is size, but not so much.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
foo
将用该const char*
做什么?如果它只是让它成为自己的c
对象,那么就没有意义了。如果要直接使用
char*
(并且现有的foo
只是从c
中取出char*
code> object),那么最好编写一个重载。What will
foo
be doing with thatconst char*
? If it's just going to make it ownc
object, then there's no point.If it is going to use the
char*
directly (and the existingfoo
just pulled thechar*
out of thec
object), then it would be better to write an overload.它不会花费零时间,所以这是你必须做出的权衡之一,速度与 API 清晰度。当然,这取决于您在采用 const char* 的函数中所做的事情,您是否正在构造 ac 对象?在这种情况下,只需提供带有 c 类接口的函数即可。
It won't take zero time so it is one of the tradeoffs you have to take, speed versus api clarity. Of course it will depend on what you are doing in your function that takes a const char*, are you constructing a c object? In which case just offer the function with the c class interface.
这类问题最好用分析器来回答。
查看它的汇编代码也可能提供线索。
This sort of question is best answered with a profiler.
Looking at the assembler code for it may also provide a clue.
这是视情况而定的。这实际上取决于给定情况下构造函数内部实际发生的情况以及代码实际执行的次数。
在您给出的示例中,这些构造函数中的操作非常简单。在任何合理的现代处理器上,这些操作都会非常非常快。因此,除非该代码每秒执行大量或更多次,否则我什至不会担心它。 (当然,“巨大”的值取决于您期望在哪种机器上运行它。对于这个构造函数,在典型的桌面处理器上,我什至不会开始担心,直到它达到至少每秒数十万次。)
如果此构造代码确实运行了很多次,那么您仍然应该对其进行分析,并确定与程序中发生的其他所有事情相比,它是否具有明显的影响。优化是一件棘手的事情。有时,你的直觉告诉你效率低下,实际上对最终结果影响不大。衡量始终是确定您实际上应该将时间花在哪里才能最有效地使程序运行得更快的方法。
It's situational. It really depends on just how much is really going on inside a constructor in a given situation and how many times that code is actually being executed.
In the example you give, those are pretty trivial operations in that constructor. On any reasonable modern processor those operations are going to be very very quick. So unless that code is being executed a huge number of times per second or more, then I wouldn't even worry about it. (Of course the value of "huge" depends on what kind of machine you expect to run this on. For this constructor, on a typical desktop processor, I wouldn't even begin to worry until it gets up into the scale of at least hundreds-of-thousands of times per second.)
If this construction code does run some huge number of times, then you still ought to profile it and determine for sure if it's having a noticeable impact compared to everything else going on in your program. Optimization is a tricky thing. Sometimes what your gut feeling says is inefficient actually has little impact on the end results. Measurement is always to way to determine where you should actually be spending your time to most effectively make your program run faster.