- mongodb - 在 MongoDB mapreduce 中,如何展平值对象?
- javascript - 对象传播与 Object.assign
- html - 输入类型 ="submit"Vs 按钮标签它们可以互换吗?
- sql - 使用 MongoDB 而不是 MS SQL Server 的优缺点
我一直在做一些 LeetCode problems ,我注意到 C 解决方案比 C++ 中完全相同的解决方案快几倍。例如:
更新了几个更简单的例子:
Given a sorted array and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order. You may assume no duplicates in the array. (Link to question on LeetCode)
我的 C 语言解决方案在 3 毫秒内运行:
int searchInsert(int A[], int n, int target) {
int left = 0;
int right = n;
int mid = 0;
while (left<right) {
mid = (left + right) / 2;
if (A[mid]<target) {
left = mid + 1;
}
else if (A[mid]>target) {
right = mid;
}
else {
return mid;
}
}
return left;
}
我的其他 C++ 解决方案,完全相同,但作为解决方案类的成员函数在 13 毫秒内运行:
class Solution {
public:
int searchInsert(int A[], int n, int target) {
int left = 0;
int right = n;
int mid = 0;
while (left<right) {
mid = (left + right) / 2;
if (A[mid]<target) {
left = mid + 1;
}
else if (A[mid]>target) {
right = mid;
}
else {
return mid;
}
}
return left;
}
};
更简单的例子:
Reverse the digits of an integer. Return 0 if the result will overflow. (Link to question on LeetCode)
C 版本在 6 毫秒内运行:
int reverse(int x) {
long rev = x % 10;
x /= 10;
while (x != 0) {
rev *= 10L;
rev += x % 10;
x /= 10;
if (rev>(-1U >> 1) || rev < (1 << 31)) {
return 0;
}
}
return rev;
}
和 C++ 版本完全一样,只是作为解决方案类的成员函数,运行时间为 19 毫秒:
class Solution {
public:
int reverse(int x) {
long rev = x % 10;
x /= 10;
while (x != 0) {
rev *= 10L;
rev += x % 10;
x /= 10;
if (rev>(-1U >> 1) || rev < (1 << 31)) {
return 0;
}
}
return rev;
}
};
我看到如果 LeetCode 测试系统没有在启用优化的情况下编译代码,那么在原始示例中使用 vector 的 vector 作为 2D 数组会有相当大的开销。但是上面更简单的例子不应该遇到这个问题,因为数据结构非常原始,尤其是在第二种情况下,你所拥有的只是长整数或整数算术。这仍然慢了三倍。
我开始认为 LeetCode 通常进行基准测试的方式可能会发生一些奇怪的事情,因为即使在整数反转问题的 C 版本中,仅替换行就会导致运行时间大幅增加 if (rev>(-1U >> 1) || rev < (1 << 31)) {和 if (rev>INT_MAX || rev < INT_MIN) {
现在,我想必须 #include<limits.h>
可能与此有关,但这个简单的更改将执行时间从 6 毫秒提高到 19 毫秒,这似乎有点极端。
最佳答案
最近我看到了vector<vector<int>>
在 C++ 中做二维数组的建议很多,我一直在向人们指出为什么这真的不是一个好主意。知道何时将临时代码拼凑在一起是一个方便的技巧,但是(几乎)从来没有任何理由将它用于真正的代码。 right thing to do是使用一个包装连续内存块的类。
所以我的第一 react 可能是指出这是差异的可能来源。但是,您也在使用 int**
在 C 版本中,这通常表示与 vector<vector<int>>
完全相同的问题.
所以我决定只比较这两种解决方案。
http://coliru.stacked-crooked.com/a/fa8441cc5baa0391
6468424
6588511
这是“C 版本”与“C++ 版本”所用的时间(以纳秒为单位)。
我的结果与您描述的差异不符。然后我想到检查人们在进行基准测试时常犯的错误
http://coliru.stacked-crooked.com/a/e57d791876b9252b
18386695
42400612
请注意,第一个示例中的 -O3 标志已变为 -O0,这会禁用优化。
结论:您可能在比较未优化的可执行文件。
C++ 支持构建不需要开销的丰富抽象,但消除开销确实需要对代码的“可调试性”造成严重破坏的某些代码转换。
这意味着调试构建避免了这些转换,因此 C++ 调试构建通常比 C 样式代码的调试构建慢,因为 C 样式代码不使用太多抽象。在计时时,例如使用函数调用代替简单存储指令的机器代码,看到上述 130% 的减速一点也不奇怪。
有些代码确实需要优化,以便即使在调试时也能获得合理的性能,因此编译器通常会提供一种应用一些优化的模式,这些优化不会给调试器带来太多麻烦。 Clang 和 gcc 使用 -O1
为此,您可以看到,即使是这种级别的优化,也基本上消除了该程序中 C 样式代码和更多 C++ 样式代码之间的差距:
http://coliru.stacked-crooked.com/a/13967ebcfcfa4073
8389992
8196935
更新:
在后面的示例中,优化不应该产生影响,因为 C++ 没有使用 C 版本所做的任何抽象。我猜想对此的解释是这些示例是使用不同的编译器或其他一些不同的编译器选项编译的。在不知道编译是如何完成的情况下,我会说比较这些运行时数字是没有意义的; LeetCode 显然没有进行苹果与苹果的比较。
关于c++ - 为什么我的 C++ 代码比 LeetCode 上的 C 代码慢三倍?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29522179/
我尝试理解[c代码 -> 汇编]代码 void node::Check( data & _data1, vector& _data2) { -> push ebp -> mov ebp,esp ->
我需要在当前表单(代码)的上下文中运行文本文件中的代码。其中一项要求是让代码创建新控件并将其添加到当前窗体。 例如,在Form1.cs中: using System.Windows.Forms; ..
我有此 C++ 代码并将其转换为 C# (.net Framework 4) 代码。有没有人给我一些关于 malloc、free 和 sprintf 方法的提示? int monate = ee; d
我的网络服务器代码有问题 #include #include #include #include #include #include #include int
给定以下 html 代码,将列表中的第三个元素(即“美丽”一词)以斜体显示的 CSS 代码是什么?当然,我可以给这个元素一个 id 或一个 class,但 html 代码必须保持不变。谢谢
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 我们不允许提问寻求书籍、工具、软件库等的推荐。您可以编辑问题,以便用事实和引用来回答。 关闭 7 年前。
我试图制作一个宏来避免重复代码和注释。 我试过这个: #define GrowOnPage(any Page, any Component) Component.Width := Page.Surfa
我正在尝试将我的旧 C++ 代码“翻译”成头条新闻所暗示的 C# 代码。问题是我是 C# 中的新手,并不是所有的东西都像 C++ 中那样。在 C++ 中这些解决方案运行良好,但在 C# 中只是不能。我
在 Windows 10 上工作,R 语言的格式化程序似乎没有在 Visual Studio Code 中完成它的工作。我试过R support for Visual Studio Code和 R-T
我正在处理一些报告(计数),我必须获取不同参数的计数。非常简单但乏味。 一个参数的示例查询: qCountsEmployee = ( "select count(*) from %s wher
最近几天我尝试从 d00m 调试网络错误。我开始用尽想法/线索,我希望其他 SO 用户拥有可能有用的宝贵经验。我希望能够提供所有相关信息,但我个人无法控制服务器环境。 整个事情始于用户注意到我们应用程
我有一个 app.js 文件,其中包含如下 dojo amd 模式代码: require(["dojo/dom", ..], function(dom){ dom.byId('someId').i
我对“-gencode”语句中的“code=sm_X”选项有点困惑。 一个例子:NVCC 编译器选项有什么作用 -gencode arch=compute_13,code=sm_13 嵌入库中? 只有
我为我的表格使用 X-editable 框架。 但是我有一些问题。 $(document).ready(function() { $('.access').editable({
我一直在通过本教程学习 flask/python http://blog.miguelgrinberg.com/post/the-flask-mega-tutorial-part-i-hello-wo
我想将 Vim 和 EMACS 用于 CNC、G 代码和 M 代码。 Vim 或 EMACS 是否有任何语法或模式来处理这种类型的代码? 最佳答案 一些快速搜索使我找到了 this vim 和 thi
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 想改进这个问题?更新问题,使其成为 on-topic对于堆栈溢出。 7年前关闭。 Improve this
这个问题在这里已经有了答案: Enabling markdown highlighting in Vim (5 个回答) 6年前关闭。 当我在 Vim 中编辑包含 Markdown 代码的 READM
我正在 Swift3 iOS 中开发视频应用程序。基本上我必须将视频 Assets 和音频与淡入淡出效果合并为一个并将其保存到 iPhone 画廊。为此,我使用以下方法: private func d
pipeline { agent any stages { stage('Build') { steps { e
我是一名优秀的程序员,十分优秀!