It has nothing to do with size. It takes extra time because a[:, 1] creates a temporary array. If you want the size of only the first dimension you should write
size(a, 1)
size and length have identical performance for arrays:
BTW: in case you brought this habit with you from another programming language, you should probably not do this in that other language either. In matlab you should write size(a, 1), exactly like in Julia, and in Python, it should be numpy.size(a, 0), or a.shape[0].
It still takes time, creating the view itself also has a cost. And anyway it is non-idiomatic, and a bit pointless, when there is already a correct way to do it.
Sometimes the performance can be the same, if the compiler realizes that it doesn’t need to actually create the view, and skips straight to getting the result. But you cannot rely on it always, and besides it is un-idiomatic and confusing to readers.
When using BenchmarkTools, you want to be careful about how you refer to variables. The key thing to remember is that BenchmarkTools is trying to measure the performance as that snippet would behave inside a function. You want to use a $ to flag the a as being a local variable instead of a global (and thus type-unstable) reference:
The above is on Julia 1.5 where views are now always non-allocating, but even on Julia 1.4 the compiler was able to see that the view wasn’t acually needed when you’re only computing the length.
I think adding a note to the NEWS entry for the above PR (that it enables non-allocating array views) would be a helpful thing to create a pull request for. Would have to be to the release-1.5 branch as I understand it; or maybe the backports-release-1.5 branch?
I know Jeff is planning on talking about that and has been working on the release notes and maybe even a blog post… note that this is the direct result of the bullet point you put it below!
Immutable structs (including tuples) that contain references can now be allocated on the stack, and allocated inline within arrays and other structs (#33886). This significantly reduces the number of heap allocations in some workloads. Code that requires assumptions about object layout and addresses (usually for interoperability with C or other languages) might need to be updated; for example any object that needs a stable address should be a mutable struct.
The most exciting part is that it’s far more general than just array views.