Golang slicing efficiency

I stumbled upon an interesting thing while checking the performance of memory allocation in GO.

package main import ( "fmt" "time" ) func main(){ const alloc int = 65536 now := time.Now() loop := 50000 for i := 0; i<loop;i++{ sl := make([]byte, alloc) i += len(sl) * 0 } elpased := time.Since(now) fmt.Printf("took %s to allocate %d bytes %d times", elpased, alloc, loop) } 

I run this on a Core-i7 2600 with a 64-bit version of go 1.6 (also similar results on 32 bits) and 16 GB of RAM (on WINDOWS 10) so when alloc is 65536 (exactly 64 KB), it works for 30 seconds (!!!!). When alloc 65535, ~ 200 ms is required. Can someone please explain this to me? I tried the same house code with my core i7-920 @ 3.8GHZ, but it did not show the same results (both took about 200 ms). Does anyone know what is going on?

+5
source share
1 answer

GOGC setting = disabled high performance (up to less than 100 ms). What for? becaue leak analysis . When you build with go build -gcflags -m , the compiler prints any selections that go into the heap. It really depends on your machine and the GO compiler version, but when the compiler decides that the distribution should move to the heap, this means 2 things: 1. The allocation will take longer (since the "allocation" on the stack is only 1 processor instruction) 2. GC will have to clear this memory later - spending more processor time for my machine, allocating 65536 bytes goes to the heap, and 65535 does not. why 1 byte changed the whole process from 200 ms to 30 seconds. Astonishing..

+4
source

Source: https://habr.com/ru/post/1245464/


All Articles