Skip to content

Commit

Permalink
Update benchmark_w_matlab.jl
Browse files Browse the repository at this point in the history
  • Loading branch information
Tortar authored Nov 28, 2024
1 parent 970d933 commit 723df90
Showing 1 changed file with 9 additions and 11 deletions.
20 changes: 9 additions & 11 deletions examples/benchmark_w_matlab.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

# # Comparing the performance of the Julia and MATLAB implementations
## Comparing the performance of the Julia and MATLAB implementations

# We can compare the performance of the Julia and MATLAB implementations
# by running the same model for the same number of epochs and measuring
Expand All @@ -22,11 +22,11 @@ parameters = BeforeIT.AUSTRIA2010Q1.parameters
initial_conditions = BeforeIT.AUSTRIA2010Q1.initial_conditions
T = 12

# we run the code to compile it first
# We run the code to compile it first
@time run(parameters, initial_conditions, T; multi_threading = false);
@time run(parameters, initial_conditions, T; multi_threading = true);

# time taken by the MATLAB code and the Generated C code with MATLAB Coder
# Time taken by the MATLAB code and the Generated C code with MATLAB Coder
# (6 threads for the parallel version), computed independently on an AMD Ryzen 5 5600H
matlab_times = [4.399592, 4.398576, 4.352314, 4.385039, 4.389989]
matlab_time = mean(matlab_times)
Expand All @@ -40,7 +40,7 @@ c_times_multi_thread = [0.305, 0.324, 0.330, 0.334, 0.323]
c_time_multi_thread = mean(c_times_multi_thread)
c_time_multi_thread_std = std(c_times_multi_thread)

# time taken by the Julia code (same platform as in the the matlab benchmarks),
# Time taken by the Julia code (same platform as in the the matlab benchmarks),
# computed as the average of 5 runs
n_runs = 5

Expand All @@ -58,13 +58,12 @@ end
julia_time_multi_thread = mean(julia_times_multi_thread)
julia_time_multi_thread_std = std(julia_times_multi_thread)

# get the number of threads used
# Get the number of threads used
n_threads = Threads.nthreads()

theme(:default, bg = :white)

# bar chart of the time taken vs the time taken by the MATLAB code, also plot the stds as error bars
# make a white background with no grid
# Bar chart of the time taken vs the time taken by the MATLAB code, also plot the stds as error bars
bar(
["MATLAB", "Gen. C, 1 thread", "Gen. C, 6 threads", "Julia, 1 thread", "Julia, $n_threads threads"],
[matlab_time, c_time, c_time_multi_thread, julia_time_1_thread, julia_time_multi_thread],
Expand All @@ -79,9 +78,8 @@ bar(
guidefont = font(6)
)

# Save the image
savefig("benchmark_w_matlab.png")

# the Julia implementation is faster than the MATLAB implementation, and the multi-threaded version is
# The Julia implementation is faster than the MATLAB implementation, and the multi-threaded version is
# faster than the single-threaded version.

# save the image
savefig("benchmark_w_matlab.png")

0 comments on commit 723df90

Please sign in to comment.