golang - container/list 배열, slice 매우 간편한 list 사용하기
이 름 : 바다아이
|
조회수 : 10101
짧은 주소 : https://www.bada-ie.com/su/?791591807754
golang 에서는 list 라는 것을 지원하는데 이게 참 속도도 빠르고 일반 slice 보다 편합니다.
타입과 상관없이 어떤 것이든 막 집어 넣을 수 있다는 장점이 있네요..
PushBack 은 뒤에 추가 PushFront 는 앞에 추가 되겠습니다.
packagemain
import(
"container/list"
"fmt"
)
funcmain() {
//새 이중 연결 리스트 생성
mylist := list.New()
// 리스트 요소 추가
mylist.PushBack("A")
mylist.PushBack(100)
mylist.PushBack(true)
mylist.PushFront("A")
// 리스트 Iteration
fore := mylist.Front(); e != nil; e = e.Next() {
fmt.Println(e.Value)
}
}
결과
A
A
100
true
속도도 일반 slice 보다 빨라요... 아래는 속도 예제 입니다.
Golang program that benchmarks container list, slice
package main
import (
"container/list"
"fmt"
"time"
"strconv"
)
func main() {
t0 := time.Now()
// Version 1: use container list.
for i := 0; i < 10000; i++ {
// New list.
values := list.New()
// Add 2 elements to the list.
values.PushBack("bird")
values.PushBack("cat")
// Add 20 elements at the front.
for i := 0; i < 20; i++ {
// Convert ints to strings.
values.PushFront(strconv.Itoa(i))
}
}
t1 := time.Now()
// Version 2: use slice.
for i := 0; i < 10000; i++ {
// New empty slice.
values := []string{}
// Add 2 elements to the slice.
values = append(values, "bird")
values = append(values, "cat")
// Add 20 elements at the front.
for i := 0; i < 20; i++ {
// Create a new slice and put the string at its start.
// ... This inserts as the front.
tempSlice := []string{}
tempSlice = append(tempSlice, strconv.Itoa(i))
// Now append all previous strings after the first one.
for x := range(values) {
tempSlice = append(tempSlice, values[x])
}
// Use the new slice.
values = tempSlice
}
}
t2 := time.Now()
// Results.
fmt.Println(t1.Sub(t0))
fmt.Println(t2.Sub(t1))
}
Results 24.4054ms container/list
119.5599ms slice
|
|
번 호
카테고리
제 목
이름
조회수
Copyright ⓒ 2001.12. bada-ie.com. All rights reserved.
이 사이트는 리눅스에서 firefox 기준으로 작성되었습니다. 기타 브라우저에서는 다르게 보일 수 있습니다.
[ Ubuntu + GoLang + PostgreSQL + Mariadb ]
서버위치 : 오라클 클라우드 춘천 실행시간 : 0.19533 초 to webmaster... gogo sea. gogo sea.