解释型语言与编译型语言相比有什么优势?

发布于 2024-08-09 18:03:35 字数 574 浏览 6 评论 0原文

可能的重复:
对动态语言的热爱是怎么回事

我已经读过这个,但我不明白。

自己做翻译有什么用呢。另外,它还提到了平台独立性。毕竟,平台独立程度受到解释器可以运行的所有平台的限制。所以,我看不出有什么优势。

另外,我什至不知道动态输入的一种好用法。我感觉没啥用。当然,我错了,因为有很多动态类型语言。

有人可以帮我吗?谢谢。

Possible Duplicate:
What’s with the love of dynamic Languages

I have already read this, but I do not get it.

What use is making your own interpreter. Also, it says platform independence. After all the degree of platform independence is limited by what all platforms your interpreter can run on. So, I don't see any advantage.

Also, I dont know even one good use of dynamic typing. It is useless I feel. Ofcourse, I am wrong, because there are so many dynamically typed languages out there.

Can somebody help me out here? thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

蓝咒 2024-08-16 18:03:35

动态语言对于简单的事情来说非常非常方便。假设您想编写一个小程序来区分两个文件。在Python中你要做的是:

file1 = open(<filename>)
file1.read()
file1.split('\n')
#same for file2
for line in file1:
    if line not in file2:
         print line

所以file1将作为FD开始。然后它变成了一根绳子。最后是一个清单。同样的 9 代码程序在 java 上至少需要两倍的行数。

当您想要编写非常大的程序并在团队中工作时,麻烦就开始了。在 java 中管理 API 和接口要容易得多。

Dynamic languages are very very convenient for simple things. Lets say you want to write a small program that will diff between two files. In python what you do is:

file1 = open(<filename>)
file1.read()
file1.split('\n')
#same for file2
for line in file1:
    if line not in file2:
         print line

so file1 will start off as a FD. Then it turns to a string. And finally a list. The same 9-code program will take at least twice as much rows on java.

The trouble starts when you want to write very large programs and work in a team. Managing APIs and interfaces is much easier in java.

季末如歌 2024-08-16 18:03:35

创建自己的解释器有什么用呢。另外,它还提到了平台独立性。毕竟,平台独立程度受到解释器可以运行的所有平台的限制。所以,我没有看到任何优势。

好吧,您使用已经存在的语言编写解释器。因此,如果您选择在多种平台(例如 Python)上运行的一个,那么在任何这些平台上,有人都可以获取您的解释器并使用它。因此,举例来说,如果您在 Windows 上编写解释器,我可以在 Linux 上运行相同的解释器,并且它将继续工作。

但是,如果您编写了编译器,则您自己必须为每个平台编写代码生成器。因此,举例来说,如果您编写的编译器可以在 Windows 上正常工作,那么当我尝试在 Linux 上运行它时,它会生成适用于 Windows 的代码,而不是 Linux 的代码,因此它无法工作。在我可以使用它之前,您需要做额外的工作,添加一个适用于 Linux 的代码生成器。

此外,我什至不知道动态类型的一项好用途。我感觉没啥用。当然,我错了,因为有很多动态类型语言。

我认为动态语言之所以流行有两个原因。

首先,它们更容易编写。类型系统需要额外的工作才能正确。

其次,由于动态语言用户不需要了解(静态)类型系统,因此许多人发现动态语言更易于使用。很多人更喜欢它们。

此外,类型语言背后的理论在过去十年中确实得到了改进。它们更加灵活和强大。因此,较旧的类型语言灵活性较差,并且类型语言通常仍然以僵化而闻名。

What use is making your own interpreter. Also, it says platform independence. After all the degree of platform independence is limited by what all platforms your interpreter can run on. So, I don't see any advantage.

Well, you write your interpreter using a language that already exists. So if you choose one that runs on may platforms (like, say, Python), then on any of those platforms someone can get your interpreter and use it. So, for example, if you write your interpreter on Windows I can run that same interpreter on Linux and it will continue to work.

However, if you had written a compiler, you yourself would have to write the code generator for each platform. So, for example, if you wrote your compiler so that it worked fine for you on Windows, when I try to run it on Linux it generates code for Windows, not Linux, so it won't work. You would need to do extra work, adding a code generator for Linux, before I could use it.

Also, I dont know even one good use of dynamic typing. It is useless I feel. Of course, I am wrong, because there are so many dynamically typed languages out there.

I think dynamic languages are popular for two reasons.

First, they are easier to write. Type systems require extra work to get right.

Second, because dynamic language users don't need to understand a (static) type system, many people find dynamic languages easier to use. So many people prefer them.

Also, the theory behind typed languages has really improved over the last decade. They are much more flexible and powerful. So older typed languages are less flexible and typed languages in general still have a reputation for being rigid.

浅笑依然 2024-08-16 18:03:35

对我来说,它是快速原型设计。虽然这些天我更加深入地研究“严肃的”Python,但我主要用它来敲定概念,然后用 C 重写。我相信 Minix 的作者宣扬大多数 C 程序通常是作为 shell 脚本诞生的(没有双关语的意思)。

我使用 PHP 来开发 Web 应用程序,以及使用数据库进行严肃业务的原型设计服务,一旦我有了一个令我满意的概念,这些服务也会用 C 重新完成。

正如我所说,我开始更加信任 Python(以及我使用它的技能),因此 Python 中诞生的一些东西将保持这种状态。但这对于我的观点来说是偶然的,我只是简单地提到它,这样我就不会惹恼 Python 爱好者:)

另一点是,它确实取决于语言。动态类型语言提供了非常诱人的快速开发前景,这对于许多需要启动和运行复杂的基于 Web 的应用程序的人来说至关重要。

除了少数例外,如果我正在编写一些绝对关键的任务,特别是在时间/时机至关重要的情况下,我会选择 C ​​或 C++。

For me, its fast prototyping. Though I have been diving more into 'serious' Python these days, I've mostly used it just to hammer out concepts that I'd later re-write in C. I believe it was the author of Minix who preached that most C programs are usually born (no pun intended) as shell scripts.

I use PHP for web apps, as well as prototyping services that do serious business with a database which are also re-done in C once I have a concept I'm happy with.

As I said, I'm beginning to trust Python more (and my skill at using it), so some stuff born in Python will just stay that way. But that's incidental to my point, I mention it simply so I don't annoy Python enthusiasts :)

Another point is, it really depends on the language. Dynamically typed languages offer a very alluring promise of rapid development, which is critical to many people who need to get complex web based applications up and running.

With few exceptions, I would pick C or C++ if I was writing something absolutely mission critical, especially where time / timing was paramount.

森林迷了鹿 2024-08-16 18:03:35

编写解释器比编写编译器更容易。 (通常)需要编写的代码较少。

要使用某种语言执行程序,您需要 (1) 阅读源代码,以便可以用某种内部格式(通常是树)表示它,并且需要 (2) 一个运行时系统。无论您使用编译器还是解释器,这些东西都是需要的。

如果您为您的语言编写一个解释器,那么添加实际的执行步骤相当容易。如果您只是执行程序的树表示,则不需要太多代码。 (但它比其他方法慢得多。)

对于编译器,您需要翻译成(通常)完全不同的语言,并且(通常)插入用于管理程序流和资源的基础设施。这是更多的工作。

It is simply easier to write an interpreter than a compiler. There is (usually) less code to write.

To execute a program in a language, you need to (1) read the source code so you can represent it in some internal format (usually a tree), and you need (2) a run-time system. Those things are needed whether you use a compiler or an interpreter.

If you write an interpreter for your language, it is fairly easy to add the actual execution step. If you just execute the tree representation of your program, it doesn't require much code. (But it will be much slower than other approaches.)

For a compiler, you need to translate into (usually) a completely different language, and also (usually) insert infrastructure for managing program flow and resources. This is much more work.

偏爱自由 2024-08-16 18:03:35

交互式 shell 是解释性语言的优点之一。这允许在编写代码时对其进行评估和测试。 “只编写一些代码”比使用希望您编写整个程序然后首先编译它的语言要快得多。从代码行数来看,差异可能不是很大,但开发过程不同。

您不一定必须在解释型和编译型之间进行选择。有些语言同时具有解释实现和编译实现,例如 Python/Jython/IronPython。这样既发挥了解释器的开发优势,又发挥了虚拟机(JRE/CLR)的性能和可移植性优势。

An interactive shell is one advantage of an interpreted language. This allows the code to be evaluated and tested as it is written. It is very much quicker to "just write some code" than in a language that expects you to write a whole program and then compile it first. In lines of code the difference might not be very great, but the development process is different.

You don't necessarily have to choose between interpreted and compiled. Some languages have both an interpreted implementation and compiled implementations e.g. Python/Jython/IronPython. This allows the development advantages of the interpreter and the performance and portability advantages of a virtual machine (JRE/CLR).

┼── 2024-08-16 18:03:35

维基百科谈论的优势是对语言实现者的优势,而不是对语言用户的优势。对于语言用户来说,最重要的缺点是解释代码的执行速度较慢。一个优点是代码可以立即执行:您不必等待编译完成。如果你的速度很快,这并不是一个真正的优势。增量编译器(例如 Lisp 编译器、F# 交互式)。对于运行时间超过几毫秒的代码,编译然后执行比解释要快。如果你的代码花费的时间少于这个时间,那么无论哪种方式都会感觉很即时。

在编译语言中完全有可能拥有动态类型、REPL、动态作用域、评估和自动内存管理。例如,C# 有动态类型和动态类型。自动内存管理、F# 和OCaml 有 REPL、自动内存管理,而 Lisp 则具备以上所有功能。

The advantages wikipedia talks about are advantages for the language implementor, not the language user. For the language user the most important disadvantage is slower execution speed of interpreted code. An advantage is that code can be executed immediately: you don't have to wait for compilation to finish. This is not really an advantage if you have a quick & incremental compiler (e.g. Lisp compilers, F# interactive). Compiling and then executing is faster than interpreting for code that runs more than a few milliseconds. If your code takes less time than that it feels instant either way.

It's perfectly possible to have dynamic typing, a REPL, dynamic scoping, eval and automatic memory management in a compiled language. For example C# has dynamic typing & automatic memory management, F# & OCaml have a REPL, automatic memory management and Lisp has all of the above.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文