# What does 'lazy' mean?

**URL:** https://discourse.julialang.org/t/what-does-lazy-mean/107247
**Category:** General Usage
**Tags:** question
**Created:** [December 7, 2023, 2:15am UTC](https://discourse.julialang.org/t/what-does-lazy-mean/107247 "2023-12-07T02:15:38Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![jiang\_ming\_zhang](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jiang_ming_zhang/32/204063_2.png) [@jiang\_ming\_zhang](https://discourse.julialang.org/u/jiang_ming_zhang)
#### Post date: [December 7, 2023, 2:15am UTC](https://discourse.julialang.org/t/what-does-lazy-mean/107247/1 "2023-12-07T02:15:38Z")

</div>

When I checked the usage of the function ‘transpose’, I got

> Lazy transpose. Mutating the returned object should appropriately mutate A. Often, but not always, yields Transpose(A), where Transpose is a lazy transpose wrapper. Note that this operation is recursive.

What does ‘lazy’ mean here? I saw a lot of ‘lazy’ and ‘eager’, but do not know what they mean.

---

<div class="post-metadata">

### Author: ![johnmyleswhite](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/johnmyleswhite/32/31_2.png) [@johnmyleswhite](https://discourse.julialang.org/u/johnmyleswhite)
#### Post date: [December 7, 2023, 2:42am UTC](https://discourse.julialang.org/t/what-does-lazy-mean/107247/2 "2023-12-07T02:42:25Z")

</div>

This is about Python, but the same ideas apply: [What is Lazy Evaluation in Python? | by Xiaoxu Gao | Towards Data Science](https://towardsdatascience.com/what-is-lazy-evaluation-in-python-9efb1d3bfed0)

---

<div class="post-metadata">

### Author: ![torrance](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/torrance/32/38990_2.png) [@torrance](https://discourse.julialang.org/u/torrance)
#### Post date: [December 7, 2023, 3:14am UTC](https://discourse.julialang.org/t/what-does-lazy-mean/107247/3 "2023-12-07T03:14:29Z")

</div>

In this particular case, it means the transpose operation doesn’t actually do anything at the time it is called, except to return the array wrapped in a Transpose object.

However, when you index into the returned transpose object, it will transparently reverse the order of the indexes, making it appear as though the underlying array has been transposed.

It is essentially a ‘view’ into the underlying array where the indices have been swapped.

So it is called lazy because it doesn’t do anything until you actually index into the result: it waits till the last minute.

The ‘eager’ alternative here is `permutedims`. This will allocate an entirely new array and transpose the values. It’s the opposite of lazy because it does all the work up front, at the expense of memory allocations and copies.

Which one is right for you? It’s really a question of performance and as always benchmark. But as a rule of thumb: if you find yourself accessing the transposed array more than a few times it’s probably best to use the eager alternative, `permutedims`.

---

<div class="post-metadata">

### Author: ![jishnub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jishnub/32/33620_2.png) [@jishnub](https://discourse.julialang.org/u/jishnub)
#### Post date: [December 7, 2023, 5:44am UTC](https://discourse.julialang.org/t/what-does-lazy-mean/107247/4 "2023-12-07T05:44:46Z")

</div>

Paradoxically, `permutedims(::AbstractVector)` goes against this idiom and is a non-materializing `reshape`. The main difference between `permutedims` and `transpose` is that the latter is recursive.
