Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

That's the first time I heard that any language that doesn't have types, is slower than a program with types. I'm unsure if that actually makes sense.

You mean slower as in performance? I'm having a hard time understanding how types == performance, so you have to mean something else.

I guess assembly would be the language you would use if you really need to squeeze out the maximum amount of performance of a single CPU, and as far as I know, it does not have types.

Also languages built on top of LLVM could get by with trading compile times for run time performance. Maybe you meant compiling gets slower without types?



Strong typing means that at runtime dynamic behaviors don't need to be accounted for.

For a compiled language, the compiler could know that `foo` and `bar` are 32-bit integers, and thus compile `foo + bar` to call the addition function for 32-bit integers. In an interpreted language, the interpreter could do the same thing.

Without that typing information, both would have to invoke a generic addition function that detects the types of its arguments at runtime, notices they're both 32-bit integers, and delegates to the corresponding addition function.

Of course there are tricks that can be played in the weakly-typed case, like assuming the same call site will always have the same types, so that there can be a short path that assumes that and only branches rarely. That way the first or first few executions might be slow, but eventually they get almost as fast as the statically-typed case. JS engines in particular and JITted runtimes in general usually do this.

>I guess assembly would be the language you would use if you really need to squeeze out the maximum amount of performance of a single CPU, and as far as I know, it does not have types.

Assembly absolutely does have types. The addition function for 32-bit ints only operates on 32-bit ints, and is distinct from the addition function for 64-bit ints.


> Assembly absolutely does have types. The addition function for 32-bit ints only operates on 32-bit ints

You can add any bits that will fit in the relevant registers. Assembly cares not whether the programmer thinks they are ints.


You should read the part of the sentence you cut off in your quote.


Dynamically typed languages still have types internally, they just look like this [0] (imagine it all implemented in assembly, without C's types, if you prefer).

[0]: https://github.com/php/php-src/blob/3709e74b5e495e210ada8039...


Great example of how bad dynamic languages represent data in memory.


Types are information about the code which enables compiler optimizations, so statically typed languages generally can be faster than dynamic languages. They aren't, always, of course, and other factors come into play.


Yes, absolutely a language without types is slower. Performance nowadays is almost always due to memory access patterns and allocations. Dynamic languages not only do not often have enough information to properly allocate memory in an efficient layout but they also tend to allocate a lot overall. Assembly is generated by languages that are compiled natively or on the fly for JIT languages. The type info is used to generate more efficient assembly.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: