- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
你好stackoverflow的用户,这是我的第一个问题,所以如果我的表达方式有任何错误,请指出,谢谢
我用Java和C++写了这个简单的计算函数
Java:
long start = System.nanoTime();
long total = 0;
for (int i = 0; i < 2147483647; i++) {
total += i;
}
System.out.println(total);
System.out.println(System.nanoTime() - start);
C++:
auto start = chrono::high_resolution_clock::now();
register long long total = 0;
for (register int i = 0; i < 2147483647; i++)
{
total += i;
}
cout << total << endl;
auto finish = chrono::high_resolution_clock::now();
cout << chrono::duration_cast<chrono::nanoseconds>(finish - start).count() << endl;
软件:- JDK8u11- 微软 Visual C++ 编译器 (2013)
结果:
java :23058430059924684811096361110
C++:23058430059924684816544374300
计算结果一样,不错然而,打印的纳米时间显示 Java 程序需要 1 秒,而在 C++ 中执行需要 6 秒
我做Java已经有一段时间了,但我是C++的新手,我的代码有什么问题吗?还是 C++ 在计算简单的情况下比 Java 慢?
另外,我在我的 C++ 代码中使用了“register”关键字,希望它能带来性能改进,但执行时间根本没有区别,有人可以解释一下吗?
编辑:我这里的错误是 C++ 编译器设置没有优化,输出设置为 x32,在应用/O2 WIN64 并删除 DEBUG 后,程序只用了 0.7 秒执行
JDK默认对输出进行优化,而VC++则不然,默认偏向于编译速度,不同的C++编译器结果也不同,有的会在编译时计算循环的结果,导致极短执行时间(大约 5 微秒)
注意:在适当的条件下,C++ 程序在这个简单的测试中会比 Java 执行得更好,但是我注意到许多运行时安全检查被跳过,违反了它作为“安全语言”的调试意图,我相信 C++ 会更好在大型数组测试中优于 Java,因为它没有边界检查
最佳答案
在 Linux/Debian/Sid/x86-64 上,使用 OpenJDK 7
// file test.java
class Test {
public static void main(String[] args) {
long start = System.nanoTime();
long total = 0;
for (int i = 0; i < 2147483647; i++) {
total += i;
}
System.out.println(total);
System.out.println(System.nanoTime() - start);
}
}
和 GCC 4.9 与
// file test.cc
#include <iostream>
#include <chrono>
int main (int argc, char**argv) {
using namespace std;
auto start = chrono::high_resolution_clock::now();
long long total = 0;
for (int i = 0; i < 2147483647; i++)
{
total += i;
}
cout << total << endl;
auto finish = chrono::high_resolution_clock::now();
cout << chrono::duration_cast<chrono::nanoseconds>(finish - start).count()
<< endl;
}
然后编译运行test.java
javac test.java
java Test
我正在获取输出
2305843005992468481
774937152
在优化编译test.cc
时
g++ -O2 -std=c++11 test.cc -o test-gcc
并运行 ./test-gcc
它运行得更快
2305843005992468481
40291
当然没有优化 g++ -std=c++11 test.cc -o test-gcc
运行速度较慢
2305843005992468481
5208949116
通过使用 g++ -O2 -fverbose-asm -S -std=c++11 test.cc
查看汇编代码,我看到编译器在编译时计算了结果:
.globl main
.type main, @function
main:
.LFB1530:
.cfi_startproc
pushq %rbx #
.cfi_def_cfa_offset 16
.cfi_offset 3, -16
call _ZNSt6chrono3_V212system_clock3nowEv #
movabsq $2305843005992468481, %rsi #,
movl $_ZSt4cout, %edi #,
movq %rax, %rbx #, start
call _ZNSo9_M_insertIxEERSoT_ #
movq %rax, %rdi # D.35007,
call _ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_ #
call _ZNSt6chrono3_V212system_clock3nowEv #
subq %rbx, %rax # start, D.35008
movl $_ZSt4cout, %edi #,
movq %rax, %rsi # D.35008, D.35008
call _ZNSo9_M_insertIlEERSoT_ #
movq %rax, %rdi # D.35007,
call _ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_ #
xorl %eax, %eax #
popq %rbx #
.cfi_def_cfa_offset 8
ret
.cfi_endproc
.LFE1530:
.size main, .-main
因此您只需要在编译器中启用优化(或切换到更好的编译器,例如GCC 4.9)
顺便说一句,Java 低级优化发生在 JIT 中的 JVM .我不太了解 JAVA,但我认为我不需要打开它们。我知道在 GCC 上你需要启用优化,这当然是提前(例如使用 -O2
)
PS:我在 21 世纪从未使用过任何 Microsoft 编译器,因此我无法帮助您如何在其中启用优化。
最后,我不认为这样的微基准测试很重要。基准然后优化您的实际应用程序。
关于java - 为什么这个简单的 C++ 添加比等效的 Java 慢 6 倍?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24848062/
我创建了一个用户可以添加测试的字段。这一切运行顺利我只希望当用户点击(添加另一个测试)然后上一个(添加另一个测试)删除并且这个显示在新字段中。 所有运行良好的唯一问题是点击(添加另一个字段)之前添加另
String[] option = {"Adlawan", "Angeles", "Arreza", "Benenoso", "Bermas", "Brebant
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 这个问题似乎不是关于 a specific programming problem, a softwar
我正在努力将 jQuery 滚动功能添加到 nav-tab (Bootstrap 3)。我希望用户能够选择他们想要的选项卡,并在选项卡内容中有一个可以平滑滚动到 anchor 的链接。这是我的代码,可
我正在尝试在用户登录后再添加 2 个 ui 选项卡。首先,我尝试做一个之后。 $('#slideshow').tabs('remove', '4'); $("#slideshow ul li:last
我有一个包含选择元素的表单,我想通过选择添加和删除其中一些元素。这是html代码(这里也有jsfiddle http://jsfiddle.net/txhajy2w/):
正在写这个: view.backgroundColor = UIColor.white.withAlphaComponent(0.9) 等同于: view.backgroundColor = UICo
好的,如果其中有任何信息,我想将这些列添加到一起。所以说我有 账户 1 2 3 . 有 4 个帐户空间,但只有 3 个帐户。我如何创建 java 脚本来添加它。 最佳答案 Live Example H
我想知道是否有一种有效的预制算法来确定一组数字的和/差是否可以等于不同的数字。示例: 5、8、10、2,使用 + 或 - 等于 9。5 - 8 = -3 + 10 = 7 + 2 = 9 如果有一个预
我似乎有一个卡住的 git repo。它卡在所有基本的添加、提交命令上,git push 返回所有内容为最新的。 从其他帖子我已经完成了 git gc 和 git fsck/ 我认为基本的调试步骤是
我的 Oracle SQL 查询如下- Q1- select hca.account_number, hca.attribute3, SUM(rcl.extended_amou
我正在阅读 http://developer.apple.com/iphone/library/documentation/iPhone/Conceptual/iPhoneOSProgrammingG
我正在尝试添加一个“加载更多”按钮并限制下面的结果,这样投资组合页面中就不会同时加载 1000 个内容,如下所示:http://typesetdesign.com/portfolio/ 我对 PHP
我遇到这个问题,我添加了 8 个文本框,它工作正常,但是当我添加更多文本框(如 16 个文本框)时,它不会添加最后一个文本框。有人遇到过这个问题吗?提前致谢。 Live Link: JAVASCRIP
add/remove clone first row default not delete 添加/删除克隆第一行默认不删除&并获取正确的SrNo(例如:添加3行并在看到问题后删除SrNo.2)
我编码this ,但删除按钮不起作用。我在控制台中没有任何错误.. var counter = 0; var dataList = document.getElementById('materi
我有一个类似数组的对象: [1:数组[10]、2:数组[2]、3:数组[2]、4:数组[2]、5:数组[3]、6:数组[1]] 我正在尝试删除前两个元素,执行一些操作,然后将它们再次插入到同一位置。
使用的 Delphi 版本:2007 你好, 我有一个 Tecord 数组 TInfo = Record Name : String; Price : Integer; end; var Info
我使用了基本的 gridster 代码,然后我声明了通过按钮添加和删除小部件的函数它工作正常但是当我将调整大小功能添加到上面的代码中时,它都不起作用(我的意思是调整大小,添加和删除小部件) 我的js代
title 323 323 323 title 323 323 323 title 323 323 323 JS $(document).keydown(function(e){
我是一名优秀的程序员,十分优秀!