0

I found a 2013 post at MATLAB Answers,The author explains with detailed examples:the zero-matrix obtained by multiplying two empty arrays faster than constructing the zero-matrix directly.

The results of my test now are different from the author's test results in 2013, the following are my test results.From the current calculation results, it seems that it is faster to directly generate a zero matrix. My computer is:I7-6700 8G RAM Win10 matlab2020a. I don't understand why space-time matrix multiplication is faster in 2013. The Accepted anwser mentioned "doesn't allocate memory (R2012a, 64bit, Win7).", does this mean that what matlab got at that time was a fake zero matrix, so it was faster?

f=@() zeros(1000)
timeit(f)
f =
  包含以下值的 function_handle:
    @()zeros(1000)
ans =1.80924e-05

g=@() zeros(1000,0)*zeros(0,1000)
timeit(g)
g =
  包含以下值的 function_handle:
    @()zeros(1000,0)*zeros(0,1000)
ans =0.0021746854
  • 3
    I think the expected behaviour would be for the built-in `zeros(1000)` to be faster, as you see now... it's not wild to imagine that MathWorks have improved their interpreter in the past 10 years to make the more fundamental command call as fast as possible, especially if other methods were flagged as faster (showing it was possible) – Wolfie Aug 29 '23 at 16:57
  • @Wolfie The result of your test is that the built-in function is faster, right? – bokabokaboka Aug 29 '23 at 17:07
  • I expect Matlab to do [the same thing than Numpy in Python](https://stackoverflow.com/a/67271140/12939557) : Matlab certainly create any fake matrix, but allocate some memory space initialized to zero and the OS can optimise this specific use-case. – Jérôme Richard Aug 29 '23 at 19:00
  • @JérômeRichard Is this similar to shallow copy and deep copy in handle classes and value classes? – bokabokaboka Aug 30 '23 at 05:00
  • @bokabokaboka Not really. This mechanism is performed by the OS itself (and the processor) thanks to virtual memory and there is no copy. It is just that the memory initialization is delayed. You pay the (expensive) cost when data is written or even read (by chunks called pages). – Jérôme Richard Aug 30 '23 at 18:59

0 Answers0