开发者

What is the advantages an interpreted language has over a compiled language? [duplicate]

This question already has answers here: Closed 13 years ago.

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.


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.


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.


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.


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.


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).


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜