《Oracle官方Java白皮书译读》之08:性能比较

《Oracle官方Java白皮书译读》之08:性能比较_第1张图片
詹姆斯·高斯林

CHAPTER 8 Performance and Comparisons

第八章 性能比较

This chapter addresses two issues of interest to prospective adopters of Java, namely, what is the performance of Java, and how does it stack up against other comparable programming languages? Let's first address the performance question and then move on to a brief comparison with other languages.

本章讨论了Java的潜在采用者感兴趣的两个问题,即Java的性能如何,以及它与其他可比较的编程语言相比如何?我们首先讨论性能问题,然后再与其他语言进行简要比较。

8.1 Performance

性能

Java has been ported to and run on a variety of hardware platforms executing a variety of operating system software. Test measurement of some simple Java programs on current high-end computer systems such as workstations and high-performance personal computers show results roughly as follows:

Java已经移植到各种硬件平台上,并在这些平台上运行各种操作系统软件。在目前的高端计算机系统如工作站和高性能个人计算机上对一些简单的Java程序进行测试,结果大致如下:

● new Object 119,000 per second
每秒创建对象119,000个

● new C() (class with several methods) 89,000 per second
new C()(有多个方法的类)每秒89,000个

● o.f() (method f invoked on object o) 590,000 per second
o.f()(对象o调用方法f)每秒590,000

● o.sf() (synchronized method sf invoked on object o) 61,500 per second
o.sf()(对象o上调用的同步方法sf)每秒61,500

Thus, we see that creating a new object requires approximately 8.4 µsec, creating a new class containing several methods consumes about 11 µsec, and invoking a method on an object requires roughly 1.7 µsec.
因此,我们看到,需要大约8.4µsec创建一个新的对象,创建一个新的类包含几个方法消耗大约11µsec,和一个对象调用一个方法需要大约1.7µsec。

While these performance numbers for interpreted bytecodes are usually more than adequate to run interactive graphical end-user applications, situations may arise where higher performance is required. In such cases, Java bytecodes can be translated on the fly (at run time) into machine code for the particular CPU on which the application is executing. This process is performed by the Just In Time (JIT) compiler. For those accustomed to the normal design of a compiler and dynamic loader, the Just In Time compiler is somewhat like putting the final machine code generator in the dynamic loader.

虽然这些解释字节码的性能数字通常足以运行交互式图形化最终用户应用程序,但也可能出现需要更高性能的情况。在这种情况下,可以动态地(在运行时)将Java字节码转换为应用程序执行所在的特定CPU的机器码。这个过程是由Just In Time (JIT)编译器执行的。对于那些习惯于编译器和动态加载器的常规设计的人来说,准时编译器有点像将最终的机器码生成器放在动态加载器中。

The bytecode format was designed with generating machine codes in mind, so the actual process of generating machine code is generally simple. Reasonably good code is produced: it does automatic register allocation and the compiler does some optimization when it produces the bytecodes. Performance of bytecodes converted to machine code is roughly the same as native C or C++.

字节码格式在设计时考虑了生成机器码,因此生成机器码的实际过程通常很简单。产生了相当好的代码:它自动分配寄存器,编译器在生成字节码时进行一些优化。转换成机器码的字节码的性能与本机C或c++大致相同。

8.2 The Java Language Compared

Java语言比较

There are literally hundreds of programming languages available for developers to write programs to solve problems in specific areas. Programming languages cover a spectrum ranging across fully interpreted languages such as UNIX Shells, awk, TCL, Perl, and so on, all the way to "programming to the bare metal" languages like C and C++.
有数百种编程语言可供开发人员编写程序来解决特定领域的问题。编程语言涵盖了一系列完全解释的语言,如UNIX shell、awk、TCL、Perl等,一直到“用最基本的语言编程”,如C和c++。

Languages at the level of the Shells and TCL, for example, are fully interpreted high-level languages. They deal with "objects" (in the sense they can be said to deal with objects at all) at the system level, where their objects are files and processes rather than data structures. Some of these languages are suitable for very fast prototyping--you can develop your ideas quickly, try out new approaches, and discard non-working approaches without investing enormous amounts of time in the process. Scripting languages are also highly portable. Their primary drawback is performance; they are generally much slower than either native machine code or interpreted bytecodes. This tradeoff may well be reasonable if the run time of such a program is reasonably short and you use the program infrequently.

例如,shell和TCL级别的语言是完全解释的高级语言。它们在系统级处理“对象”(从某种意义上说,它们可以处理所有对象),其中它们的对象是文件和进程,而不是数据结构。其中一些语言适合非常快速的原型设计——您可以快速地开发您的想法,尝试新的方法,并抛弃无效的方法,而不需要在这个过程中投入大量的时间。脚本语言也是高度可移植的。它们的主要缺点是性能;它们通常比本机机器码或解释的字节码慢得多。如果这样一个程序的运行时间相当短,并且您不经常使用该程序,那么这种权衡可能是合理的。

In the intermediate ground come languages like Perl, that share many characteristics in common with Java. Perl's ongoing evolution has led to the adoption of object-oriented features, security features, and it exhibits many features in common with Java, such as robustness, dynamic behavior, architecture neutrality, and so on.

在中间阶段出现了像Perl这样的语言,它们与Java有许多共同的特征。Perl正在进行的发展导致了面向对象特性、安全特性的采用,而且它与Java有许多共同的特性,如健壮性、动态行为、体系结构中立性等。

At the lowest level are compiled languages such as C and C++, in which you can develop large-scale programming projects that will deliver high performance. The high performance comes at a cost, however. Drawbacks include the high cost of debugging unreliable memory management systems and the use of multithreading capabilities that are difficult to implement and use. And of course when you use C++, you have the perennial fragile superclass issue. Last but definitely not least, the binary distribution problem of compiled code becomes unmanageable in the context of heterogeneous platforms all over the Internet.

最底层是C和c++之类的编译语言,在这些语言中,您可以开发大型编程项目,从而提供高性能。然而,高性能是有代价的。缺点包括调试不可靠的内存管理系统的高成本,以及使用难以实现和使用的多线程功能。当然,当您使用c++时,您会遇到持久的易碎超类问题。最后但绝不是最不重要的一点是,在Internet上各种异构平台的上下文中,已编译代码的二进制分布问题变得难以管理。

The Java language environment creates an extremely attractive middle ground between very high-level and portable but slow scripting languages and very low level and fast but non-portable and unreliable compiled languages. The Java language fits somewhere in the middle of this space. In addition to being extremely simple to program, highly portable and architecture neutral, the Java language provides a level of performance that's entirely adequate for all but the most compute-intensive applications.

Java语言环境在非常高级和可移植但速度很慢的脚本语言和非常低级和快速但不可移植且不可靠的编译语言之间创建了一个非常有吸引力的中间地带。Java语言正好处在这个空间的中间。除了程序极其简单、高度可移植性和体系结构中立之外,Java语言还提供了除计算密集型应用程序外完全适合所有应用程序的性能级别。

Prospective adopters of the Java language need to examine where the Java language fits into the firmament of other languages. Here is a basic comparison chart illustrating the attributes of the Java language--simple, object-oriented, threaded, and so on--as described in the earlier parts of this paper.

Java语言的潜在采用者需要研究Java语言在其他语言中的地位。下面是一个基本的比较图表,它演示了Java语言的属性——简单的、面向对象的、线程化的等等——如本文前面部分所述。

《Oracle官方Java白皮书译读》之08:性能比较_第2张图片
Java语言的属性

From the diagram above, you see that the Java language has a wealth of attributes that can be highly beneficial to a wide variety of developers. You can see that Java, Perl, and SmallTalk are comparable programming environments offering the richest set of capabilities for software application developers.

从上面的图表中,您可以看到Java语言具有丰富的属性,这些属性对各种开发人员都非常有益。您可以看到,Java、Perl和SmallTalk是可比较的编程环境,为软件应用程序开发人员提供了最丰富的功能集。

8.3 A Major Benefit of Java: Fast and Fearless Prototyping

Java的主要优点:快速而无畏的原型设计

Very dynamic languages like Lisp, TCL, and SmallTalk are often used for prototyping. One of the reasons for their success at this is that they are very robust--you don't have to worry about freeing or corrupting memory.

非常动态的语言,如Lisp、TCL和SmallTalk,通常用于原型设计。它们在这方面成功的原因之一是它们非常健壮——您不必担心释放或破坏内存。

Similarly, programmers can be relatively fearless about dealing with memory when programming in Java. The garbage collection system makes the programmer's job vastly easier; with the burden of memory management taken off the programmer's shoulders, storage allocation errors go away.

类似地,在用Java编程时,程序员可以相对大胆地处理内存。垃圾收集系统大大简化了程序员的工作;随着内存管理的重担从程序员肩上卸下,存储分配错误也就消失了。

Another reason commonly given that languages like Lisp, TCL, and SmallTalk are good for prototyping is that they don't require you to pin down decisions early on--these languages are semantically rich.

通常认为Lisp、TCL和SmallTalk等语言适合于原型设计的另一个原因是,它们不需要您在早期确定决策——这些语言在语义上非常丰富。

Java has exactly the opposite property: it forces you to make explicit choices. Along with these choices come a lot of assistance--you can write method invocations and, if you get something wrong, you get told about it at compile time. You don't have to worry about method invocation error.

Java有完全相反的属性:它强迫您做出明确的选择。伴随这些选择而来的是大量的帮助——您可以编写方法调用,如果出现错误,您可以在编译时了解它。您不必担心方法调用错误。

8.4 Summary

总结

From the discussion above, you can see that the Java language provides high performance, while its interpreted nature makes it the ideal development platform for fast and fearless prototyping. From the previous chapters, you've seen that the Java language is extremely simple and object oriented. The language is secure to survive in the network-based environment. The architecture-neutral and portable aspects of the Java language make it the ideal development language to meet the challenges of distributing dynamically extensible software across networks.

从上面的讨论中,您可以看到Java语言提供了高性能,而其解释的特性使其成为快速而大胆地构建原型的理想开发平台。在前面几章中,您已经看到Java语言非常简单,并且是面向对象的。这种语言在基于网络的环境中是安全的。Java语言的体系结构中立和可移植方面使其成为满足跨网络动态分发可扩展软件的挑战的理想开发语言。

Now We Move On to the HotJava World-Wide Web Browser
现在我们转向HotJava全球网络浏览器

These first eight chapters have been your introduction to the Java language environment. You've learned about the capabilities of Java and its clear benefits to develop software for the distributed world. Now it's time to move on to the next chapter and take a look at the HotJava World-Wide Web browser--a major end-user application developed to make use of the dynamic features of the Java language environment.

前八章是对Java语言环境的介绍。您已经了解了Java的功能及其为分布式世界开发软件的明显好处。现在该进入下一章了,我们来看看HotJava worldwide Web browser,这是一个主要的终端用户应用程序,它是为了利用Java语言环境的动态特性而开发的。


好好学习,天天向上!继续下一章...


快速导航:

《Oracle官方Java白皮书译读》概览

《Oracle官方Java白皮书译读》之01:Java技术简介

《Oracle官方Java白皮书译读》之02:简单和友好的

《Oracle官方Java白皮书译读》之03:面向对象

《Oracle官方Java白皮书译读》之04:体系结构中立、可移植和健壮的

《Oracle官方Java白皮书译读》之05:解释执行和动态的

《Oracle官方Java白皮书译读》之06:安全性

《Oracle官方Java白皮书译读》之07:多线程

《Oracle官方Java白皮书译读》之08:性能比较

《Oracle官方Java白皮书译读》之09:基本系统和库

《Oracle官方Java白皮书译读》之10:HotJava万维网浏览器

《Oracle官方Java白皮书译读》之11:延伸阅读


你可能感兴趣的:(《Oracle官方Java白皮书译读》之08:性能比较)