The Price of Scripting

programming
performance

(pico) #1

This is going to be short.

Taking as starting point the last code challenge posted by @oaktree ( Programming Challenge #6 thanks @oaktree! ) and just out of curiosity, I decided to take some memory performance numbers. This challenge was pretty straight forward and it does not looks like any of the posted solution could be farther optimized. So, I checked the memory resources used by them.

These is what I have got in my system as provided by the gnome-system-monitor and bringing up the Properties window for the different versions of the program:

Language | Memory  | Virt Memory | Res Memory | Wr Memory | Sh Memory
---------+---------+-------------+------------+-----------+-----------
Java     | 8.4 MiB |   5.5 GiB   |   15.6 MiB |   8.4 MiB |   7.5 MiB
Python   | 2.5 MiB |  23.7 MiB   |    4.8 MiB |   2.5 MiB |   2.3 MiB
Perl     | 744 KiB |  26.4 MiB   |    2.6 MiB |   744 KiB |   1.9 MiB
lua      | 212 KiB |  13.8 MiB   |    1.0 MiB |   212 KiB |   840 Kib
C++      | 176.KiB |  12.4 MiB   |    1.0 MiB | 176.0 KiB | 900.0 KiB
C        | 100 KiB |   4.2 MiB   |  352.0 KiB | 100.0 KiB | 276.0 KiB
C-diet   |  20 KiB | 172.0 KiB   |    4.0 KiB |  20.0 KiB |   N/A

Some comments to better understand the table.

I modify the programs to wait for a key press before they end so I could capture the numbers in the table.
C-diet is the C version compiled with dietLibC and therefore it is a static binary and that is the reason it does not share memory.

Java version was compiled with OpenJDK

Columns in the table are:

  • Virt Memory: Virtual memory.
  • Res Memory: Resident Memory.
  • Wr Memory: Writable Memory.
  • Sh Memory: Shared Memory.

There are pretty good explanations in the Internet for these parameters in case you are curious about its exact meaning.

Scripting languages are great and, for many different problems, they are the right solution. But that power and flexibility comes with a price that is good to understand.


Programming Challenge #6
The Price of Scripting: DietLibC vs ASM
(oaktree) #2

What I was unaware of for a while is that modern scripting languages like Python and Ruby are actually compiled to bytecode when run (rather than interpreted [google the difference between MRI and YARV, two Ruby implementations]).

The thing is, this runtime compilation brings about considerable overhead, as your numbers show.

Also, if you look at the C++ implementation, which I don’t think could be farther optimized, it’s interesting to see how much more memory it uses compared to C. I think the C++ STL is probably to blame, although there is much controversy surrounding its effects on program performance.

We may need to use larger test sizes to help see where all the memory is going.


#3

Again I’m amazed! Keep the great work up mate!


(Command-Line Ninja) #4

Wow! I knew scripting languages are hungry, but I didn’t know by that much! Since python compiles to bytecode rather than being re-compiled at runtime (similar to @oaktree’s comment), I’d be interested to see how Java stacks up, since Java is always regarded as being slow and clunky; however they operate on the same compliation and runtime paradime.

As for C-diet I’m incredibly impressed, I didn’t know C-diet was a thing! Seems like a space-rocket type deal.

Nice work Pico! :wink:


(pico) #5

@oaktree I think this overhead does not come much from the compilation, but all the high level constructors. Whenever you write one python line that doe a lot of stuff… it is actually doing a lot of stuff under the hood… because, at the end, the processor does what it can do… some basic logic/arithmetic operations, moving data around, jumping and that is roughly it.

I think that the same happens to C++. The standard library is just big, and the trade-off between memory usage and speed is well-known.

And I also agree with you that this samples are not representative for the general case. I think they are interesting for cases where a small tool for a modest platform has to be used… 2Mb in a router may be quite some memory…

Java is not much popular around here, but it a different beast. In addition to the original bytecode compilation (original as first… it was not original when Sun introduced it… yes Sun), Java soon integrated a JIT compiler and later on the so-called Hot-Spot.

The JIT compiler actually compiles the bytes codes to real machine code. The hotspot is in principle, able to analyze the program at run-time and decide which parts are used more frequently (the hotspot) and selectively compile only what is required. Advocates of this approach usually claims that run-time optimizations can be better than compile-time optiomizations, as more information is available.

But all that is speed related… memory-wise, it use to take a look, I do not know nowadays.

@Cromical are you working in a bot for the forum???


#6

Working on a bot you say? No, not really. Although I am looking at an old Internet Worm.


(oaktree) #7
Language | Memory  | Virt Memory | Res Memory | Wr Memory | Sh Memory
---------+---------+-------------+------------+-----------+-----------
Java     | 8.4 MiB |   5.5 GiB   |   15.6 MiB |   8.4 MiB |   7.5 MiB
Python   | 2.5 MiB |  23.7 MiB   |    4.8 MiB |   2.5 MiB |   2.3 MiB
Perl     | 744 KiB |  26.4 MiB   |    2.6 MiB |   744 KiB |   1.9 MiB
lua      | 212 KiB |  13.8 MiB   |    1.0 MiB |   212 KiB |   840 Kib
C++      | 176.KiB |  12.4 MiB   |    1.0 MiB | 176.0 KiB | 900.0 KiB
C        | 100 KiB |   4.2 MiB   |  352.0 KiB | 100.0 KiB | 276.0 KiB
C-diet   |  20 KiB | 172.0 KiB   |    4.0 KiB |  20.0 KiB |   N/A

Wait, why does Java take up 5.5 gigabytes of Virtual Memory!?


(pico) #8

That is what I’ve got in one of my machines. Just tried in another, number are a bit better, but still far away from the rest

8.9 MiB | 2.9MiB | 16.5MiB | 8.9 MiB | 8.1 MiB

Note that the virtual memory is, in principle, memory that has been requested to the OS but that has not been used yet.


(Command-Line Ninja) #9

I’d be interested to see how C-diet stands up to ASM…


The Price of Scripting: DietLibC vs ASM
(system) #10

This topic was automatically closed after 30 days. New replies are no longer allowed.