logo

Julia's Powerful Convenience Features, Macros 📂Julia

Julia's Powerful Convenience Features, Macros

Overview

Macros in Julia provide convenience features when coding, being executed in front of a scope. For example, if you want to know how much time your program is consuming, you can write it as follows.

@time for t in 1:10
    foo()
    bar()
end

Examples

There are many types, but the following macros are especially widely used:

  • @time: Measures the execution time of the function or scope that follows. When it’s unclear how to optimize in some situations, measuring the time first makes it easier to choose the better option. In some languages, writing code to measure the time can be cumbersome, but in Julia, a single macro tells you not only the execution time but also the amount of memory used.
  • @.: Adds a dot (.) to the operations in the subsequent expression.
  • @threads: A macro that makes it easier to implement parallel processing.
  • @animate: A macro that easily bakes GIFs.

Environment

  • OS: Windows
  • julia: v1.5.0