什么事(或在何种情况下),可以比C使C ++慢? [英] What things (or in what cases) can make C++ slower than C ?

查看:144
本文介绍了什么事(或在何种情况下),可以比C使C ++慢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个面试问题,面试已经完成。

This is an interview question, the interview has been done.

什么东西能比C使C ++慢?

What things can make C++ slower than C ?

面试官问它很深,总是问:什么事吗?每当我说了些什么。

The interviewer asked it very deep and always asked "anything else ? " whenever I said something.

我的思路:

C ++功能无法在C语言中可能有一定的成本。

C++ features not available in C may have some cost.

例如,如果我们使用的分配不是由初始化列表来初始化类的构造函数中的成员,该成员的默认构造函数可一旦构造函数体之前调用,然后该值被分配消灭了。

For example, if we use assignment to initialize class's members inside a constructor not by the initialization list, the member's default constructor may be called once before the body of the constructor, and then that value wiped out by the assignment.

虚函数需要通过搜索虚函数指针调用。这是一个额外开销。

Virtual functions need to be called by searching virtual function pointer. This is a overhead.

任何更好的想法?

任何帮助将AP preciated。

Any help will be appreciated.

谢谢!

推荐答案

没有。事实上,C ++是快于C.自从相比的std ::排序的qsort

Nothing. In fact, C++ is faster than C. Ever compared std::sort to qsort?

人们说,虚函数花费时间打电话。他们是这样。但这样做的C相当于一个虚函数表仰视。如果您在两种语言编写等价逻辑,C ++版本会更容易维护,清洁,更快。

People say that virtual functions cost time to call. They do. But so does the C equivalent of looking up in a vtable. If you write equivalent logic in both languages, the C++ version will be more maintainable, cleaner, and faster.

编辑:哦,是的,你可以调用C 的printf ++如果你愿意,或完全重新做,如果你想流实施

Oh yeah, you can call printf from C++ if you want, or completely re-do the stream implementation if you want.

和我提到这崩溃的程序由于错位空终结表现还算无关紧要?

And did I mention that the performance of a program which crashes due to a misplaced NULL terminator is fairly immaterial?

宏和内联函数会膨胀是C的可执行文件一样必将为模板,将在C ++。

Macros and inline functions will "bloat" a C executable just as surely as templates will in C++.

这篇关于什么事(或在何种情况下),可以比C使C ++慢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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