什么是字节code在本土code优点? [英] What are advantages of bytecode over native code?

查看:206
本文介绍了什么是字节code在本土code优点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好像什么可以做,以字节为code,你可以做很容易和更快的本土code。从理论上讲,你甚至可以保持语言和平台的独立性,分布在字节code程序和库然后编译为本机code在安装,而不是JIT编译它。

因此​​,在一般情况下,当你想执行字节code,而不是本地?

解决方案

汉克Shiffman从SGI说(很久以前,但它直到真正的):

  

有Java的三个优点   使用字节code,而不是去   该系统的本机code:

     
      
  1. 便携:每一种计算机都有其独特的指令   组。虽然一些处理器包括   他们的predecessors说明,   这是一般真实的程序   上一种计算机的运行   将不会在任何其他运行。添加在   由操作所提供的服务   系统,其中每个系统描述   自己独特的方式,和你有一个   兼容性问题。一般情况下,   不能写和编译程序   一种制度,并在任何运行   其他没有大量的工作。 Java的获取   解决此限制通过插入   其之间的虚拟机   应用和真实环境   (电脑+操作系统)。如果   应用程序编译成Java字节   code和字节code是国际preTED   在每一个环境中以同样的方式再   你可以写一个程序,   将在所有的不同   其中,支持Java的平台。   (这是理论,反正在   实践总是有小   不兼容趴在等待   程序员。)

  2.   
  3. 安全:一个Java的优点是它集成到Web。加载   即使用Java到您的网页   浏览器和Java code是   自动下载和执行。   但是,如果code销毁文件是什么,   无论是通过恶意或草率   程序员的一部分吗? Java的   prevents从做下载的applet   任何破坏性通过禁止   潜在的危险操作。   之前,它允许code运行它   检查它企图绕过   安全。它验证数据   坚持使用:code表示   操纵数据项作为一个整数   在某一阶段,然后尝试使用它   作为一个指针后会被捕获并   prevented的执行。 (Jav​​a的   语言不允许指针   运算,所以你不能编写Java   code做什么,我们刚才所描述的。   然而,没有什么prevent   有人从编写的破坏性字节   使用十六进制code自己   编辑甚至建立一个Java字节   code和汇编)。它一般不   可以分析程序的   机code执行之前和   确定它是否做任何事情   坏。喜欢写作技巧   自修改code意味着邪恶   操作可能甚至不存在,直到   后来。但Java字节code的设计   对于这种验证:它   不具有指令的   恶意程序员用隐藏   他们的进攻。

  4.   
  5. 尺寸:在微处理器世界RISC一般是preferable   在CISC。最好是有一个小   指令集和使用许多快速   说明做的一项工作,而不是有   许多复杂的操作实现   单指令。 RISC设计   需要更少的门电路芯片上集成了   落实他们的指示,使   为管道和其他更多的空间   技术,使每一个指令   快点。在帧间preTER,但是,   这一切都不重要。如果你想   实施为单个指令   具有可变switch语句   长度取决于案件的数量   条款,没有理由不这样做   所以。事实上,复杂指令集   是一个优点为基于网络的   语言:它意味着同样的   方案将变小(少   的更大的复杂性指示),   这意味着更少的传输时间   在我们的速度,有限的网络。

  6.   

因此​​,考虑字节code VS原生的时候,考虑你想要的便携性,安全性,大小和执行速度之间要进行哪些权衡。如果速度是唯一重要的因素,入乡随俗。如果任何人都更重要的是,去与字节code。

我还可以补充一点,维持了一系列的操作系统,并为每一个版本相同的code基础架构为目标合辑可能会变得非常乏味。这是一个巨大的胜利用在多个平台上使用相同的Java字节code和拥有它只是工作。

It seems like anything you can do with bytecode you can do just as easily and much faster in native code. In theory, you could even retain platform and language independence by distributing programs and libraries in bytecode then compiling to native code at installation, rather than JITing it.

So in general, when would you want to execute bytecode instead of native?

解决方案

Hank Shiffman from SGI said (a long time ago, but it's till true):

There are three advantages of Java using byte code instead of going to the native code of the system:

  1. Portability: Each kind of computer has its unique instruction set. While some processors include the instructions for their predecessors, it's generally true that a program that runs on one kind of computer won't run on any other. Add in the services provided by the operating system, which each system describes in its own unique way, and you have a compatibility problem. In general, you can't write and compile a program for one kind of system and run it on any other without a lot of work. Java gets around this limitation by inserting its virtual machine between the application and the real environment (computer + operating system). If an application is compiled to Java byte code and that byte code is interpreted the same way in every environment then you can write a single program which will work on all the different platforms where Java is supported. (That's the theory, anyway. In practice there are always small incompatibilities lying in wait for the programmer.)

  2. Security: One of Java's virtues is its integration into the Web. Load a web page that uses Java into your browser and the Java code is automatically downloaded and executed. But what if the code destroys files, whether through malice or sloppiness on the programmer's part? Java prevents downloaded applets from doing anything destructive by disallowing potentially dangerous operations. Before it allows the code to run it examines it for attempts to bypass security. It verifies that data is used consistently: code that manipulates a data item as an integer at one stage and then tries to use it as a pointer later will be caught and prevented from executing. (The Java language doesn't allow pointer arithmetic, so you can't write Java code to do what we just described. However, there is nothing to prevent someone from writing destructive byte code themselves using a hexadecimal editor or even building a Java byte code assembler.) It generally isn't possible to analyze a program's machine code before execution and determine whether it does anything bad. Tricks like writing self-modifying code mean that the evil operations may not even exist until later. But Java byte code was designed for this kind of validation: it doesn't have the instructions a malicious programmer would use to hide their assault.

  3. Size: In the microprocessor world RISC is generally preferable over CISC. It's better to have a small instruction set and use many fast instructions to do a job than to have many complex operations implemented as single instructions. RISC designs require fewer gates on the chip to implement their instructions, allowing for more room for pipelines and other techniques to make each instruction faster. In an interpreter, however, none of this matters. If you want to implement a single instruction for the switch statement with a variable length depending on the number of case clauses, there's no reason not to do so. In fact, a complex instruction set is an advantage for a web-based language: it means that the same program will be smaller (fewer instructions of greater complexity), which means less time to transfer across our speed-limited network.

So when considering byte code vs native, consider which trade-offs you want to make between portability, security, size, and execution speed. If speed is the only important factor, go native. If any of the others are more important, go with bytecode.

I'll also add that maintaining a series of OS and architecture-targeted compilations of the same code base for every release can become very tedious. It's a huge win to use the same Java bytecode on multiple platforms and have it "just work."

这篇关于什么是字节code在本土code优点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆