Created
April 21, 2020 20:24
-
-
Save dgrr/5e0f8e5c1eca53fc5e5fd1a28a1e1cab to your computer and use it in GitHub Desktop.
QuickXML processing a 233MB XML file.
This file contains hidden or 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 ( | |
"fmt" | |
"log" | |
"os" | |
"runtime" | |
"strings" | |
xml "github.com/dgrr/quickxml" | |
) | |
func main() { | |
file, err := os.Open(os.Args[1]) | |
if err != nil { | |
log.Fatalln(err) | |
} | |
defer file.Close() | |
var ( | |
readNext = false | |
count = 0 | |
) | |
PrintMemUsage() | |
r := xml.NewReader(file) | |
for r.Next() { | |
switch e := r.Element().(type) { | |
case *xml.StartElement: | |
readNext = e.NameUnsafe() == "location" | |
case *xml.TextElement: | |
if readNext && strings.Contains(string(*e), "Africa") { | |
count++ | |
readNext = false | |
} | |
} | |
} | |
runtime.GC() | |
PrintMemUsage() | |
fmt.Println("counter =", count) | |
} | |
func PrintMemUsage() { | |
var m runtime.MemStats | |
runtime.ReadMemStats(&m) | |
// For info on each, see: https://golang.org/pkg/runtime/#MemStats | |
fmt.Printf("Alloc = %v MiB", bToMb(m.Alloc)) | |
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 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment