Created
November 8, 2017 02:56
-
-
Save fujimoto-s/64e45882cc566708bd0afbf67c726333 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"bufio" | |
"fmt" | |
"github.com/armon/go-radix" | |
"math/rand" | |
"os" | |
"time" | |
) | |
func main() { | |
trie := radix.New() | |
// Insert | |
var elapsed int64 | |
count := 0 | |
all := 128 * 128 * 128 | |
for b := 0; b < 128; b++ { | |
for c := 0; c < 128; c++ { | |
for d := 0; d < 128; d++ { | |
count += 1 | |
fmt.Printf("\rInserting: %d%%", count*100/all) | |
addr := fmt.Sprintf("%d.%d.%d.0/24", b, c, d) | |
start := time.Now() | |
trie.Insert(addr, "some routes") | |
end := time.Now() | |
elapsed += end.Sub(start).Nanoseconds() | |
} | |
} | |
} | |
fmt.Printf("\nInsertion 128^3 routes: %dns\n", elapsed) | |
fmt.Printf(" Average: %dns\n", elapsed/int64(all)) | |
// Search | |
rand.Seed(time.Now().UnixNano()) | |
elapsed = 0 | |
for i := 0; i < 10000; i++ { | |
a := rand.Intn(128) | |
b := rand.Intn(128) | |
c := rand.Intn(128) | |
addr := fmt.Sprintf("%d.%d.%d.0/24", a, b, c) | |
start := time.Now() | |
trie.Get(addr) | |
end := time.Now() | |
elapsed += end.Sub(start).Nanoseconds() | |
} | |
fmt.Printf("Search 10000 times: %dns\n", elapsed) | |
fmt.Printf(" Average: %dns\n", elapsed/10000) | |
fmt.Print("Press 'Enter' to quit...") | |
bufio.NewReader(os.Stdin).ReadBytes('\n') | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment