I tested exactly same julia code in MacBookAir 2020(M1, RAM: 8GB) and MaMini 2020(M1, RAM 16GB).
The code is
using LaTeXStrings
using DelimitedFiles
using Printf
using GZip
using Statistics
using LinearAlgebra
using PyCall
using Plots
py"""
import numpy as np
import pickle as pk
def load_comm():
with open("./pr_comm.pk", "rb") as f:
com_p = pk.load(f)
with open("./cn_comm.pk", "rb") as f:
com_c = pk.load(f)
com = [0 for _ in range(len(com_p) + len(com_c))]
for i in range(0,len(com_p)*2,2):
if i % 8 < 4:
com[i] = np.array(com_p[i//2])
com[i+1] = np.array(com_c[i//2])
else:
com[i] = np.array(com_c[i//2])
com[i+1] = np.array(com_p[i//2])
com = np.array(com)
return com
"""
function load_sample(N::Int64, sam::String, K::Float64, n::Int64)
path_step = @sprintf "./%d_step%s_n_%d_Kr_%.2lf.gz" N sam n K
step = GZip.open(path_step, "r") do file
# Read the file and parse it with readdlm
readdlm(file, ',', Float64, '\n')
end
return step
end
function reshape_step(step::Array{Float64, 2}, N::Int64)
step_reshape = permutedims(reshape(step', N*2+5, 1002, 1000), [2,1,3]);
return step_reshape
end
function load_and_process_sample(N::Int64, sam::String, K::Float64, n::Int64)
step = load_sample(N, sam, K, n)
step_reshape = reshape_step(step, N)
omega = step_reshape[:,6:N+5,:]
ke = omega.^2 .* 0.5
time_ = step_reshape[:,3,1]
time_[3:end] = time_[3:end]./100;
return time_, ke
end
function plot_ke(ke, time_, comm_elem)
p1 = plot(time_, ke[:,1,1],title = "Perturbed Node 0", xtick = [])
p2 = plot(time_, ke[:,comm_elem[1,2:end],1],title = "Perturbed Node's Community", xtick = [])
p3 = plot(time_, ke[:,comm_elem[2,1:end],1],title = "Nearest Community", xtick = [])
p3 = plot!(time_, ke[:,comm_elem[5,1:end],1])
p4 = plot(time_, ke[:,comm_elem[3,1:end],1], xlabel = "Time (s)",title = "Second Community", xtick = [])
p4 = plot!(time_, ke[:,comm_elem[6,1:end],1])
p4 = plot!(time_, ke[:,comm_elem[9,1:end],1])
p = plot(p1, p2, p3, p4, layout = (4,1),legend = false, grid = false, ylabel = "KE",size=(700,800),
fmt = :png, dpi=300, xlabelformat = :plain)
return p
end
elapsed = @elapsed begin
N = 144
K = 6.0
n = 0
comm = py"load_comm"
comm_elem = comm() .+ 1
samples = ["00"]
titles = ["RegularLattice"]
for (i, sam) in enumerate(samples)
print("Loading samples\n")
time_, ke = load_and_process_sample(N, sam, K, n)
println("loaded sample", sam)
p = plot_ke(ke, time_, comm_elem)
savefig(p, "./$(titles[i]).png")
end
end
println("Elapsed time: ", elapsed, " seconds")
The result in MacMini is about 53 seconds, and MacBook is about 203 seconds…
I cannot understand this difference…
I tested in VS code terminal with julia [filename].jl
. The loaded file has about 800MB.