Skip to content

Instantly share code, notes, and snippets.

@CharlyCst
Created March 20, 2020 17:02
Show Gist options
  • Save CharlyCst/df5c5bc15316f3ff61884f67bb3e2ad6 to your computer and use it in GitHub Desktop.
Save CharlyCst/df5c5bc15316f3ff61884f67bb3e2ad6 to your computer and use it in GitHub Desktop.
package main
import (
"container/list"
"time"
"strconv"
"fmt"
lb "gosb"
)
func init() {
// Initialize litterbox
lb.Initialize(lb.MPK_BACKEND)
}
func main() {
sandbox["", ""]() {
fmt.Println("//// Memalloc SB ////")
n := 1000 // Number of repetitions
t0 := time.Now()
// Version 1: use container list.
for i := 0; i < n; 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()
fmt.Println("//// Linked list passed ////")
// Version 2: use slice.
for i := 0; i < n; 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()
fmt.Println("//// Slice test passed ////")
fmt.Println(t1.Sub(t0))
fmt.Println(t2.Sub(t1))
fmt.Println("/// Memalloc SB Done ////")
}()
fmt.Println("Done")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment