I want to do a transformation which may depends on a large number of columns. The MWE & benchmark codes:
using DataFrames
df = DataFrame(rand(10, 50), :auto)
cols = propertynames(df)
# due to the design of my actual codes, this function can only accept a vector.
f(x) = sum(x)
for i in 1:length(cols)
@time select(df, cols[1:i] => ByRow((x...) -> f([x...])))
end
which outputs
0.047720 seconds (106.18 k allocations: 7.179 MiB, 98.71% compilation time)
0.053221 seconds (160.25 k allocations: 10.859 MiB, 99.49% compilation time)
0.075922 seconds (103.81 k allocations: 7.002 MiB, 69.57% gc time, 99.78% compilation time)
0.058375 seconds (175.07 k allocations: 11.871 MiB, 99.52% compilation time)
0.028908 seconds (127.15 k allocations: 8.693 MiB, 99.41% compilation time)
0.031616 seconds (136.75 k allocations: 9.333 MiB, 99.47% compilation time)
0.034097 seconds (147.99 k allocations: 10.086 MiB, 99.53% compilation time)
0.044063 seconds (159.05 k allocations: 10.874 MiB, 9.65% gc time, 99.60% compilation time)
0.038997 seconds (170.84 k allocations: 11.680 MiB, 99.47% compilation time)
0.041796 seconds (182.76 k allocations: 12.447 MiB, 99.56% compilation time)
0.116322 seconds (268.54 k allocations: 18.050 MiB, 3.57% gc time, 99.79% compilation time)
0.114697 seconds (260.76 k allocations: 17.541 MiB, 99.78% compilation time)
0.126955 seconds (279.34 k allocations: 18.755 MiB, 99.81% compilation time)
0.146084 seconds (300.26 k allocations: 20.103 MiB, 2.70% gc time, 99.83% compilation time)
0.160667 seconds (324.04 k allocations: 21.459 MiB, 99.83% compilation time)
0.178767 seconds (348.90 k allocations: 22.756 MiB, 2.17% gc time, 99.84% compilation time)
0.194160 seconds (374.26 k allocations: 24.147 MiB, 99.86% compilation time)
0.228985 seconds (394.74 k allocations: 25.180 MiB, 1.55% gc time, 99.86% compilation time)
0.242908 seconds (421.90 k allocations: 26.493 MiB, 1.63% gc time, 99.87% compilation time)
0.265827 seconds (451.13 k allocations: 27.959 MiB, 99.88% compilation time)
0.298992 seconds (480.59 k allocations: 29.472 MiB, 1.27% gc time, 99.90% compilation time)
0.339485 seconds (511.92 k allocations: 31.351 MiB, 1.21% gc time, 99.90% compilation time)
0.372290 seconds (547.37 k allocations: 33.242 MiB, 99.91% compilation time)
0.413722 seconds (586.02 k allocations: 34.857 MiB, 0.83% gc time, 99.92% compilation time)
0.460433 seconds (625.47 k allocations: 36.655 MiB, 0.79% gc time, 99.92% compilation time)
0.505811 seconds (666.13 k allocations: 38.678 MiB, 0.71% gc time, 99.93% compilation time)
0.568882 seconds (704.31 k allocations: 40.326 MiB, 0.70% gc time, 99.94% compilation time)
0.624337 seconds (741.10 k allocations: 41.929 MiB, 0.63% gc time, 99.94% compilation time)
0.705504 seconds (787.13 k allocations: 43.825 MiB, 0.59% gc time, 99.95% compilation time)
0.768546 seconds (833.44 k allocations: 45.764 MiB, 0.53% gc time, 99.95% compilation time)
0.854407 seconds (880.88 k allocations: 47.704 MiB, 0.50% gc time, 99.95% compilation time)
0.294726 seconds (943.01 k allocations: 65.202 MiB, 1.37% gc time, 99.84% compilation time)
0.256508 seconds (317.29 k allocations: 19.898 MiB, 1.39% gc time, 99.79% compilation time)
0.220710 seconds (302.03 k allocations: 18.543 MiB, 99.74% compilation time)
0.213203 seconds (301.65 k allocations: 18.546 MiB, 1.74% gc time, 99.70% compilation time)
0.209549 seconds (304.92 k allocations: 18.678 MiB, 99.64% compilation time)
0.210791 seconds (311.05 k allocations: 18.899 MiB, 1.74% gc time, 99.60% compilation time)
0.216300 seconds (318.56 k allocations: 19.257 MiB, 99.55% compilation time)
0.219012 seconds (322.26 k allocations: 19.498 MiB, 1.61% gc time, 99.54% compilation time)
0.215500 seconds (325.18 k allocations: 19.426 MiB, 99.51% compilation time)
0.216434 seconds (331.18 k allocations: 19.765 MiB, 99.48% compilation time)
0.224192 seconds (338.62 k allocations: 20.162 MiB, 1.57% gc time, 99.47% compilation time)
0.222419 seconds (341.59 k allocations: 20.252 MiB, 99.41% compilation time)
0.233205 seconds (344.93 k allocations: 20.258 MiB, 1.50% gc time, 99.40% compilation time)
0.239102 seconds (348.26 k allocations: 20.308 MiB, 99.33% compilation time)
0.235739 seconds (355.91 k allocations: 20.656 MiB, 99.25% compilation time)
0.243609 seconds (362.59 k allocations: 21.027 MiB, 99.27% compilation time)
0.250321 seconds (368.78 k allocations: 21.378 MiB, 1.79% gc time, 99.26% compilation time)
0.242530 seconds (375.07 k allocations: 21.664 MiB, 99.24% compilation time)
0.252716 seconds (382.77 k allocations: 21.949 MiB, 1.48% gc time, 99.21% compilation time)
As can be seen, most of time is compilation time. And the situation gets worse as number of dependent columns increases to some point (i=31), when i>31, the running time decreases in half. I don’t quite understand what is going on. If I run the same for loop twice, the situation is the same.
How can I improve the speed of this kind of operation? I have to run such select
a million times.