Skip to content

preallocate actually has more (while not by number of bytes) allocations! #22

@PallHaraldsson

Description

@PallHaraldsson

FYI: I was looking into this package, and ran the code from the README, and (at least on Julia 1.5) it has two extra allocations.

[From the tag-line: "Remember what memory we needed last time and use it gain every time after", better to change to "to gain" and finish with a period?]

Since preallocation wasn't clearly (much) faster, I was thinking isn't that the point (and to have less/or not variability for real-time), maybe the timing wasn't reliable, so I checked:

julia> @btime f(A, B, C);
  33.333 ms (4 allocations: 15.26 MiB)

julia> @btime preallocated_f(A, B, C);
  27.981 ms (6 allocations: 272 bytes)

julia> @btime preallocated_f(A, B, C);
  38.364 ms (6 allocations: 272 bytes)

julia> @btime preallocated_f(A, B, C);
  21.913 ms (6 allocations: 272 bytes)

julia> @btime f(A, B, C);
  22.533 ms (4 allocations: 15.26 MiB)

I guess this is also useful for GPUs, some such way needed there?

Is a better example needed to show the usefulness? FYI2, I tried with a larger arrays (and I think it mostly shows how fast/good Julia's GC is):

julia> A, B, C = (rand(10000, 10000) for _ in 1:3)
Base.Generator{UnitRange{Int64},var"#14#15"}(var"#14#15"(), 1:3)

[..]

julia> @btime f(A, B, C);
  23.648 s (4 allocations: 1.49 GiB)

julia> @btime preallocated_f(A, B, C);
  22.597 s (6 allocations: 272 bytes)

julia> @time preallocated_f(A, B, C);
 22.931816 seconds (6 allocations: 272 bytes)

julia> @time f(A, B, C);
 22.840137 seconds (4 allocations: 1.490 GiB)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions