java c 转换,自动Java到C ++转换

自动将Java代码转换为C++可能会导致性能下降,因为两种语言有不同的性能特点。例如,Java中频繁创建对象对性能影响小,而C++中的内存分配则相对昂贵。直接转换往往结合了两者缺点,且不易维护。最佳实践是根据目标语言的特性进行手动优化。

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.

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

}

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" ) );

}

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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值