Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How many times len() runs?

Tags:

go

How many times will len(p) run? Only one and the result will be saved, or will it run for each iteration?

func main() {
    p := []int{2, 3, 5, 7, 11, 13}
    for i:=0;i<len(p);i++ {}
}
like image 859
Tyranron Avatar asked Feb 02 '13 13:02

Tyranron


People also ask

Is Len () constant time?

The runtime complexity of the len() function on your Python list is O(1). It takes constant runtime no matter how many elements are in the list. Why? Because the list object maintains an integer counter that increases and decreases as you add and remove list elements.

How does LEN () work in Python?

The len() function returns the number of items in an object. When the object is a string, the len() function returns the number of characters in the string.

What does the LEN () function do?

The function len() is one of Python's built-in functions. It returns the length of an object. For example, it can return the number of items in a list. You can use the function with many different data types.

Is Len () function works only for?

In financial analysis, the LEN function can be useful if we wish to get the length of a given text string as the number of characters. LEN will also count characters in numbers, but number formatting is not included.


2 Answers

len() on a slice is optimized by the compiler, it's like accessing a local variable. It's not really a function call.

You can verify with:

$ cat x.go
package main
import "fmt"
func main() {
    a := []int{1,2, 3}
    fmt.Println(len(a))
}

And then look at the compiler output:

$ go tool 6g -S x.go

--- prog list "main" ---
0000 (x.go:3) TEXT    main+0(SB),$128-0
0001 (x.go:4) MOVQ    $0,autotmp_0002+-80(SP)
0002 (x.go:4) MOVQ    $0,autotmp_0002+-72(SP)
0003 (x.go:4) MOVQ    $0,autotmp_0002+-64(SP)
0004 (x.go:4) LEAQ    autotmp_0002+-80(SP),BX
0005 (x.go:4) MOVQ    BX,autotmp_0001+-56(SP)
0006 (x.go:4) MOVQ    autotmp_0001+-56(SP),BX
0007 (x.go:4) MOVQ    statictmp_0000+0(SB),BP
0008 (x.go:4) MOVQ    BP,(BX)
0009 (x.go:4) MOVQ    statictmp_0000+8(SB),BP
0010 (x.go:4) MOVQ    BP,8(BX)
0011 (x.go:4) MOVQ    statictmp_0000+16(SB),BP
0012 (x.go:4) MOVQ    BP,16(BX)
0013 (x.go:4) MOVQ    autotmp_0001+-56(SP),BX
0014 (x.go:4) MOVQ    $3,CX
0015 (x.go:5) LEAQ    autotmp_0005+-16(SP),DI
0016 (x.go:5) MOVQ    $0,AX
0017 (x.go:5) STOSQ   ,
0018 (x.go:5) STOSQ   ,
0019 (x.go:5) LEAQ    autotmp_0005+-16(SP),BX
0020 (x.go:5) MOVQ    BX,autotmp_0004+-48(SP)
0021 (x.go:5) MOVQ    autotmp_0004+-48(SP),BX
0022 (x.go:5) MOVQ    $1,SI
0023 (x.go:5) MOVQ    $1,DX
0024 (x.go:5) MOVQ    BX,autotmp_0003+-40(SP)
0025 (x.go:5) MOVQ    autotmp_0003+-40(SP),BX
0026 (x.go:5) MOVQ    $type.int+0(SB),AX
0027 (x.go:5) MOVQ    AX,(BX)
0028 (x.go:5) MOVQ    CX,8(BX)
0029 (x.go:5) MOVQ    autotmp_0003+-40(SP),BX
0030 (x.go:5) MOVQ    BX,(SP)
0031 (x.go:5) MOVQ    SI,8(SP)
0032 (x.go:5) MOVQ    DX,16(SP)
0033 (x.go:5) CALL    ,fmt.Println+0(SB)
0034 (x.go:6) RET     ,

Notice the CALL to fmt.Println, but no call to len.

like image 113
Brad Fitzpatrick Avatar answered Oct 06 '22 02:10

Brad Fitzpatrick


Try an experiment! http://play.golang.org/p/Eksb6bQovC

Or read the docs: http://golang.org/ref/spec#For_statements (TL;DR: "The condition is evaluated before each iteration.")

In your example, len is part of the condition so it is evaluated before each iteration. There will be an iteration for the six times the condition evaluates to true, then one more evaluation where it evaluates to false and the for loop exits. So, seven times in all.

like image 35
Sonia Avatar answered Oct 06 '22 02:10

Sonia