# For Monte Carlo simulation with same code same algorithm, how fast is Julia compared with Fortran?

**URL:** <https://discourse.julialang.org/t/for-monte-carlo-simulation-with-same-code-same-algorithm-how-fast-is-julia-compared-with-fortran/67021>\
**Category:** General Usage\
**Tags:** fortran, performance\
**Created:** [August 26, 2021, 7:44am UTC](https://discourse.julialang.org/t/for-monte-carlo-simulation-with-same-code-same-algorithm-how-fast-is-julia-compared-with-fortran/67021 "2021-08-26T07:44:49Z")\
**Posts on this page:** 1\
**Showing post:** 41

<div class="post-metadata">

**Author:** ![genkuroki](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/genkuroki/32/18030_2.png) [@genkuroki](https://discourse.julialang.org/u/genkuroki)\
**Post date:** [August 29, 2021, 5:26am UTC](https://discourse.julialang.org/t/for-monte-carlo-simulation-with-same-code-same-algorithm-how-fast-is-julia-compared-with-fortran/67021/41 "2021-08-29T05:26:27Z")

</div>

You can define a macro to effortlessly unpack a large number of values at once from a single object.

Sample code:

```julia
struct Foo{A, B, C} a::A; b::B; c::C end

"""
`@unpackall_Foo(obj)` unpacks all fields of the object `obj` of type `Foo`.
"""
macro unpackall_Foo(obj)
    names = fieldnames(Foo)
    Expr(:(=),
        Expr(:tuple, names...),
        Expr(:tuple, (:($obj.$name) for name in names)...)
    ) |> esc
end

@doc @unpackall_Foo

```

> `@unpackall_Foo(obj)` unpacks all fields of the object `obj` of type `Foo` .

Input:

```julia
@macroexpand @unpackall_Foo foo

```

Output:

```julia
:((a, b, c) = (foo.a, foo.b, foo.c))

```

Input:

```julia
@unpackall_Foo Foo(1, 2.0, "three")
a, b, c

```

Output:

```julia
(1, 2.0, "three")

```

With this macro, there is no need to share a large number of global constants between multiple functions.

Just pass the object `foo`, which contains the values shared between multiple functions, to a function as an argument each time, and unpack it in the function.

Input:

```julia
foo = Foo(1, 2.0, "three")

function f(foo::Foo)
    @unpackall_Foo foo
    @show a b c
    return
end

f(foo)

```

Output:

```julia
a = 1
b = 2.0
c = "three"

```

In the above, we unpack only `a`, `b`, and `c` from `foo`, but you can increase the number of them as much as you want.

To increase the number of fields in the style of `struct Foo{A, B, C} a::A; b::B; c::C end` as much as you want, you can use [ConcreteStructs.jl](https://github.com/jonniedie/ConcreteStructs.jl) ([my example](https://discourse.julialang.org/t/function-depending-on-the-global-variable-inside-module/64322/18)).

Jupyter notebook: [https://github.com/genkuroki/public/blob/main/0018/How%20to%20define%20unpacking%20macros.ipynb](https://github.com/genkuroki/public/blob/main/0018/How%20to%20define%20unpacking%20macros.ipynb)

---

_[View the full topic](https://discourse.julialang.org/t/for-monte-carlo-simulation-with-same-code-same-algorithm-how-fast-is-julia-compared-with-fortran/67021)._
