I am currently working on mathematica script on a Raspberry Pi, which will periodically (10 secs) be launched by a bash script. However the runtime of my script really isn't the best.
It's actually around 19-20 secs with a CPU load of 25%. I noticed that only one CPU core is used for my calculations so I tried to parallel the "bottlenecks" in my script with Parallelize[]
. Now I achieve a CPU load of 100%, however the runtime extends to 23 secs! (launching the kernels takes more time than I win). Additionaly I'm not sure which commands are worth to be processed in parallel.
One data file (256kb) contains 16000 samples per variabel (2 variables:volt, ampere). I tried to limit the amount of samples imported (I actually only need about 5000) to enhance the runtime with methods suggested here. But still seems as the whole file gets imported.
How can I improve the import effectively? Are there any additional improvements possible which enhance my runtime?
#!/usr/bin/wolfram -script
Print["Mathematica : Importing Config"]
cali = StringSplit[Import["/grapher/calibration.plt"], {"\n","="} ]
fileconfig = StringSplit[Import["/grapher/realtime_config.plt"], "'"]
filepath = fileconfig[[2]] <> fileconfig[[4]] <> ".csv"
Print["Mathematica : Importing Data"]
Paralellize[data = Import[filepath,"Table"]]
Print["Mathematica : Calculating Phaseshift"]
filenumber = StringSplit[fileconfig[[4]], "_"];
filenumber = StringSplit[filenumber[[2]], "."];
filenumber = filenumber[[1]];
voltcal = ToExpression[cali[[4]]];
amperecal = ToExpression[cali[[6]]] * voltcal ;
fv = data[[All, 1]];
fi = data[[All, 2]];
Paralellize[ffts1 = Fourier[fv, FourierParameters -> {-1, 1}];]
Paralellize[ffts2 = Fourier[fi, FourierParameters -> {-1, 1}];]
max = Max[Abs[ffts1]];
pos = First[First[Position[Abs[ffts1], max]]];
shiftrad = Arg[ffts1[[pos]]] - Arg[ffts2[[pos]]];
shiftdegree = shiftrad *180 /Pi;
shiftcos = Cos[shiftrad *-1];
shiftrad = ToString[shiftrad];
shiftdegree = ToString[shiftdegree];
shiftcos = ToString[shiftcos];
ologname = "logname='" <> fileconfig[[4]] <> "'";
odatetime = "datetime='" <> fileconfig[[8]] <> "'";
ophaserad = "phaserad='" <> shiftrad <> "'";
ophasedgr = "phasedgr='" <> shiftdegree <> "'";
ocos = "cos='" <> shiftcos <> "'" ;
oumax = "umax='" <> ToString[Max[fv]* voltcal] <> "'";
oumin = "umin='" <> ToString[Min[fv]* voltcal] <> "'";
oimax = "imax='" <> ToString[Max[fi]* amperecal] <> "'";
oimin = "imin='" <> ToString[Min[fi]* amperecal] <> "'";
outputname = fileconfig[[2]] <> "math/math_" <> filenumber <> ".dat";
Export[outputname, {ologname, odatetime , ophaserad , ophasedgr,
ocos, oumax, oumin, oimax, oimin}]
Read
operations are typically faster thanImport
, assuming its a simple file structure. $\endgroup$