Last active
August 29, 2015 14:01
-
-
Save MirkoDziadzka/a941b46e0b66035f1129 to your computer and use it in GitHub Desktop.
Python vs. Go performance ... reading a set of values from a file
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
import time | |
def readFile(filename): | |
res = {} | |
for line in open(filename).read().split("\n"): | |
if line: | |
res[line] = True | |
return res | |
if __name__ == '__main__': | |
start_time = time.time() | |
res = readFile("hash.db") | |
duration = time.time() - start_time | |
print "reading %d entries in %f seconds" % (len(res), duration) |
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 ( | |
"fmt" | |
"os" | |
"time" | |
"bytes" | |
) | |
type set map[string]bool | |
func readFile(filename string) set { | |
file, err := os.Open(filename) | |
if err != nil { | |
panic(err) | |
} | |
stat, err := file.Stat() | |
if err != nil { | |
panic(err) | |
} | |
buffer := make([]byte, stat.Size()) | |
_,err = file.Read(buffer) | |
if err != nil { | |
panic(err) | |
} | |
res := make(set) | |
for _,value := range bytes.Split(buffer, []byte("\n")) { | |
s := string(value) | |
if s != "" { | |
res[s] = true | |
} | |
} | |
return res | |
} | |
func main() { | |
start_time := time.Now() | |
res := readFile("hash.db") | |
duration := time.Now().Sub(start_time) | |
fmt.Printf("read %d entries in %s\n", len(res), duration) | |
} |
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
import time | |
def readFile(filename): | |
res = set() | |
for line in open(filename).read().split("\n"): | |
if line: | |
res.add(line) | |
return res | |
if __name__ == '__main__': | |
start_time = time.time() | |
res = readFile("hash.db") | |
duration = time.time() - start_time | |
print "reading %d entries in %f seconds" % (len(res), duration) |
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
import hashlib | |
def makeSet(size): | |
res = set() | |
for i in range(size): | |
value = hashlib.sha1(str(i)).hexdigest() | |
res.add(value) | |
return res | |
def makeFile(filename, size): | |
data = makeSet(size) | |
open(filename,"w").write("\n".join(data) + '\n') | |
if __name__ == '__main__': | |
makeFile("hash.db", 1000 * 1000) |
A couple things you can try (not sure how much they will help):
- Use a
map[string]struct{}
. For a large set, this should reduce the overall size noticeably. - If you can test on the current go1.3, get rid of
s
and usemap[string(value)]
, which is now free (no allocation).
Also noting that most of the time is in the map assignment may be a red herring, unless you compare that directly to the amount of time that python spends in set assignment.
Ok ... the hint on the mailing list was give a size hint to make: So the cache-improved.go is doing this and is now on the same performance level as the Python implementation.
Go improved output: read 1000000 entries in 468.880726ms
package main
import (
"fmt"
"os"
"time"
"bytes"
)
type set map[string]struct{}
func readFile(filename string) set {
file, err := os.Open(filename)
if err != nil {
panic(err)
}
stat, err := file.Stat()
if err != nil {
panic(err)
}
buffer := make([]byte, stat.Size())
_,err = file.Read(buffer)
if err != nil {
panic(err)
}
splitBuffer := bytes.Split(buffer, []byte("\n"))
res := make(set, len(splitBuffer))
for _,value := range splitBuffer {
s := string(value)
if s != "" {
res[s] = struct{}{}
}
}
return res
}
func main() {
start_time := time.Now()
res := readFile("hash.db")
duration := time.Now().Sub(start_time)
fmt.Printf("read %d entries in %s\n", len(res), duration)
}
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
GO output: read 1000000 entries in 790.16865ms
Python output: reading 1000000 entries in 0.470706 seconds
Python output with dict: reading 1000000 entries in 0.554953 seconds
The Python code is twice as fast as the Go code. After some debugging, it seems that the performance problem in the Go code is the
data structure. Is there a better way to implement this in Go?
Btw: please note that the Go program spends most of the time in
Changing the file read routine to a buffered io and ReadLine does not improve the performance