Automatic Java to C++ conversion [duplicate]

*爱你&永不变心* 提交于 2019-12-10 23:55:04

问题


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/


回答1:


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.




回答2:


The positive point to that conversion is that you will need a proper object oriented design in order to switch from java to C++ (paradigm intersection).

However some people say that coding C++ does not bring speed improvement compared to java code.




回答3:


Even if that worked, I am not so sure that you will see much speed improvement. Java's Hotspot JIT compiler has become pretty good.




回答4:


It's nearly impossible to replace the automatic memory-management of Java with a manual memory-management via a program. So you most likely will end up with a program, that has memory leaks or with C++-code that uses a garbage-collector. But a garbage-collector in Java have much more things to rely on (for instance no pointer-arithm,etics), so a garbage-collector in C++ to be safe it has a decreased performance. So your automatic conversion will most likely decrease performance.

Instead try to port it by hand to C++ or optimize the Java-code.




回答5:


There is hardly a chance that this type of conversion shell lead to better performances. Usually when the JVM is working, it converts most of the code to native machine code. what you are suggesting is converting the Jave code to C++ and from there to native machine code, that is to add an extra phase. However there are some trivial cases in which some gain may be achived due to the fact that:

1) It takes some time to load the JVM from scratch.

2) It takes some time to do the Jit, when running a very short program, a lot of times, you may prefer to waste this time before running.

3) You may not got the same level of machine code on Java, if you are not running on Server mode. (On server Mode you are expected to get top notch machine code, and also one that is best suited for your own CPU as detected on run-time, that is usually lacking on most C/C++ implantations of programs, and moreover a machine code optimized at run-time)




回答6:


The languages have such different styles in usage that a mindless conversion would be of little use. An intelligent converter would be next to imposable to write because of the different styles used.

Some Problem areas:

  • Resource allocation is controlled by 'try{} finally{}' blocks in Java while C++ uses RAII.
  • Java does exception checking at compile time C++ run-time.
  • Exceptions are handled differently. Two exceptions:
    • In Java (last throw is propagated)
    • In C++ application terminated.
  • Java has a massive standard library
    C++ has all the same functionality you just need to go find it on the web [which is a pain].
  • Java uses pointers for everything.
    • A straight unthinking conversion will leave you with a program consisting of just shared_ptr objects.

Anyway with JIT Compilation Java is comparable to C++ in speed.




回答7:


Speaking of such converters in general, they can't be expected to produce either maintainable or high-performance code, since those generally require having an actual human who understands the language writing the code. What they are very useful for is in making it easy to port languages. Any language with a converter to C, for example, can be swiftly implemented on a wide range of languages. I used f2c in the mid-90s to run a Fortran routine on a Macintosh.

You may or may not have performance improvements if you rewrite the code in C++. You'll probably get a slowdown if you use an automatic converter.



来源:https://stackoverflow.com/questions/1096615/automatic-java-to-c-conversion

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!