Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 60 additions & 0 deletions benchmark/bfs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package main

import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"time"
)

type Site struct {
Title string `json:"title"`
Body string `json:"body"`
Links []int `json:"links"`
ParentSite string `json:"parent_site"`
SiteURL string `json:"site_url"`
LinkURL []string `json:"link_urls"`
Slug string `json:"slug"`
}

func main() {
// fmt.Println("Hello, World!")
jsonFile, err := os.Open("../site/data/site_data.json")
if err != nil {
fmt.Println(err)
}
// fmt.Println("opened jsonfile")

defer jsonFile.Close()
byteValue, _ := ioutil.ReadAll(jsonFile)
var sites []Site
json.Unmarshal(byteValue, &sites)

var visited [1000000]int
t1 := time.Now()
for i := 0; i < len(sites); i++ {
if visited[i] == 0 {
queue := []int{i}
queueBeg := 0
for queueBeg < len(queue) {

for size := len(queue) - queueBeg; size > 0; size-- {
front := queue[queueBeg]
queueBeg++
if visited[front] == 1 {
continue
}
visited[front] = 1
// fmt.Println("visiting", front)
for j := 0; j < len(sites[front].Links); j++ {
queue = append(queue, sites[front].Links[j])
}
}
}
}
}
t2 := time.Now()
elapsed := t2.Sub(t1)
fmt.Println(elapsed)
}
58 changes: 58 additions & 0 deletions benchmark/dfs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package main

import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"time"
)

type Site struct {
Title string `json:"title"`
Body string `json:"body"`
Links []int `json:"links"`
ParentSite string `json:"parent_site"`
SiteURL string `json:"site_url"`
LinkURL []string `json:"link_urls"`
Slug string `json:"slug"`
}

func main() {
// fmt.Println("Hello, World!")
jsonFile, err := os.Open("../site/data/site_data.json")
if err != nil {
fmt.Println(err)
}
// fmt.Println("opened jsonfile")

defer jsonFile.Close()
byteValue, _ := ioutil.ReadAll(jsonFile)
var sites []Site
json.Unmarshal(byteValue, &sites)

var visited [1000000]int
var stack []int
t1 := time.Now()
for i := 0; i < len(sites); i++ {
if visited[i] == 0 {
stack = append(stack, i)
for len(stack) > 0 {
top := stack[len(stack)-1]
// pop here
stack = stack[:len(stack)-1]
if visited[top] == 0 {
visited[top] = 1
// fmt.Println("visiting", top)
for j := 0; j < len(sites[top].Links); j++ {
stack = append(stack, sites[top].Links[j])
}
}
}

}
}
t2 := time.Now()
elapsed := t2.Sub(t1)
fmt.Println(elapsed)
}
165 changes: 165 additions & 0 deletions benchmark/map.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
package main

import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"regexp"
"runtime"
"strings"
//"time"
)

type Site struct {
Title string `json:"title"`
Body string `json:"body"`
Links []int `json:"links"`
ParentSite string `json:"parent_site"`
SiteURL string `json:"site_url"`
LinkURL []string `json:"link_urls"`
Slug string `json:"slug"`
}

const (
ALBHABET_SIZE = 26
)

/*
type trieNode struct {
childrens [ALBHABET_SIZE]*trieNode
isWordEnd bool
}

type trie struct {
root *trieNode
}

func initTrie() *trie {
return &trie{
root: &trieNode{},
}
}

func (t *trie) insert(word string) {
wordLength := len(word)
current := t.root
for i := 0; i < wordLength; i++ {
index := word[i] - 'a'
if current.childrens[index] == nil {
current.childrens[index] = &trieNode{}
}
current = current.childrens[index]
}
current.isWordEnd = true
}

func (t *trie) find(word string) bool {
wordLength := len(word)
current := t.root
for i := 0; i < wordLength; i++ {
index := word[i] - 'a'
if current.childrens[index] == nil {
return false
}
current = current.childrens[index]
}
if current.isWordEnd {
return true
}
return false
}
*/
func PrintMemUsage() {
var m runtime.MemStats
runtime.ReadMemStats(&m)
// For info on each, see: https://golang.org/pkg/runtime/#MemStats
fmt.Printf("HeapAlloc = %v MiB", bToMb(m.HeapAlloc))
fmt.Printf("\tTotalAlloc = %v MiB", bToMb(m.TotalAlloc))
fmt.Printf("\tSys = %v MiB", bToMb(m.Sys))
fmt.Printf("\tNumGC = %v\n", m.NumGC)
}

func bToMb(b uint64) uint64 {
return b / 1024 / 1024
}

func main() {
jsonFile, err := os.Open("../site/data/site_data.json")
if err != nil {
fmt.Println(err)
}

defer jsonFile.Close()
byteValue, _ := ioutil.ReadAll(jsonFile)
var sites []Site
json.Unmarshal(byteValue, &sites)

myMap := make(map[string]bool)
// trie := initTrie()
//var sum time.Duration

//no_of_words, err := strconv.Atoi(os.Args[1])
//k := 0
//fl := 0
runtime.GC()
PrintMemUsage()
for i := 0; i < len(sites); i++ {
sentence := strings.Split(sites[i].Body, " ")
for j := 0; j < len(sentence); j++ {
sentence[j] = strings.ToLower(sentence[j])
matched, _ := regexp.MatchString(`^[a-z]*$`, sentence[j])
// fmt.Println(matched, sentence[j])
// t1 := time.Now()
if matched {
//trie.insert(sentence[j])
// myMap[sentence[j]] = append(myMap[sentence[j]], sites[i].SiteURL)
myMap[sentence[j]] = true
//k++
}
/*if k >= no_of_words+100 {
fl = 1
break
}
*/
// t2 := time.Now()
// elapsed := t2.Sub(t1)
// sum += elapsed
}
}
runtime.GC()
PrintMemUsage()

// t1 := time.Now()
/*
k = 0
fl = 0
for i := 0; i < len(sites); i++ {
sentence := strings.Split(sites[i].Body, " ")
for j := 0; j < len(sentence); j++ {
sentence[j] = strings.ToLower(sentence[j])
matched, _ := regexp.MatchString(`^[a-z]*$`, sentence[j])
if matched {
t1 := time.Now()
if myMap[sentence[j]] {
}
t2 := time.Now()
elapsed := t2.Sub(t1)
sum += elapsed
k++
if k >= no_of_words {
fl = 1
break
}
}
}
if fl == 1 {
break
}
}
*/
//fmt.Println(sum)

// t2 := time.Now()
// fmt.Println(elapsed)
}
Loading