自动Java到C ++转换 [英] Automatic Java to C++ conversion
问题描述
有没有人尝试过自动Java到C ++转换速度的改进?是长期的维修噩梦吗?
只读,用于在Gecko中生成HTML5解析引擎 http:// ejohn .org / blog / html-5-parsing /
Has anyone tried automatic Java to C++ conversion for speed improvements? Is it a maintenance nightmare in the long run? Just read that is used to generate the HTML5 parsing engine in Gecko http://ejohn.org/blog/html-5-parsing/
推荐答案
一般来说,不会是一个改进。不同的语言有不同的成语,影响性能。
In general, automatic conversions from one language to another will not be an improvement. Different languages have different idioms that affect performance.
最简单的例子是循环和变量创建。在Java GC世界中,使用new创建对象几乎是免费的,并且它们同样容易被遗忘。在C ++内存分配是(一般来说)昂贵:
The simplest example is with loops and variable creation. In a Java GC world, creating objects with new is almost free, and they dive into oblivion just as easily. In C++ memory allocation is (generally speaking) expensive:
// Sample java code
for ( int i = 0; i < 10000000; ++i )
{
String str = new String( "hi" ); // new is free, GC is almost free for young objects
}
直接转换C ++会导致性能不佳(使用TR1 shared_ptr作为内存处理程序而不是GC):
Direct conversion to C++ will result in bad performance (use of TR1 shared_ptr as memory handler instead of GC):
for ( int i = 0; i < 10000000; ++i )
{
std::shared_ptr< std::string > str( new std::string( "hi" ) );
}
用C ++编写的等效循环为:
The equivalent loop written in C++ would be:
for ( int i = 0; i < 10000000; ++i )
{
std::string str( "hi" );
}
从一种语言直接翻译到另一种语言通常以两个世界的最差和难以维护代码。
Direct translation from a language to another usually ends with the worst of both worlds and harder to maintain code.
这篇关于自动Java到C ++转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!