26

Whether or not I compile a Racket program seems to make no difference to the runtime performance.

Is it just the loading of the file initially that is improved by compilation? In other words, does running racket src.rkt do a jit compilation on the fly, which is why I see no difference in compiling vs interactive?

Even for tight loops of integer arithmetic, where I thought some difference would occur, the profile times are equivalent whether or not I previously did a raco make.

Am I missing something simple?

PS, I notice that I can run racket against the source file (.rkt) or .zo file. Does racket automatically use the .zo if one is found that corresponds to the .rkt file, or does the .zo file need to be used explicitly? Either way, it makes no difference to the performance numbers I'm seeing.

Sam Tobin-Hochstadt
  • 4,983
  • 1
  • 21
  • 43
Scott Klarenbach
  • 37,171
  • 15
  • 62
  • 91

2 Answers2

30

Yes, you're right.

Racket compiles code in two stages: first, the code is compiled into bytecode form, and then when it runs it gets jitted into machine code. When you compile a file, you're basically creating the bytecode which saves on re-compiling it later. Since that's usually not something that takes a lot of time for small pieces of code, you won't see any noticeable difference in runtimes. For an extreme example, you can delete all *.zo files in the collection tree and start DrRacket -- it will take a lot of time to start since there's a ton of code, but once it does start, it would run almost as usual. (It would also be slow to click "run" since that will reload and recompile some files.) Another concern for bigger pieces of code is that the compilation process can make memory consumption higher, but that's also not an issue with smaller pieces of code.

See also the Performace chapter in the guide for hints on how to improve performance.

Eli Barzilay
  • 29,301
  • 3
  • 67
  • 110
15

Racket will always compile your code, regardless of whether it is run interactively at the REPL or run from the command-line. Here is the section in the guide that explains it. In interactive mode, the compiler turns every expression/definition into bytecode in memory and executes that. Otherwise, the compilers outputs the bytecode to zo files.

Note: Eli replied at the same time I did. See his response for more details.

Asumu Takikawa
  • 8,447
  • 1
  • 28
  • 43