Skip to content

Instantly share code, notes, and snippets.

@nnao45
Last active October 14, 2017 13:13
Show Gist options
  • Select an option

  • Save nnao45/6c442631f7d1fa3e367d803d6fa429ce to your computer and use it in GitHub Desktop.

Select an option

Save nnao45/6c442631f7d1fa3e367d803d6fa429ce to your computer and use it in GitHub Desktop.
findコマンドが遅いからgoで書き直してみた件 ref: http://qiita.com/A_Resas/items/aa22af6df3c8b6a89409
$ sudo time find / -name "*client*" > /dev/null
real 0m1.503s
user 0m0.980s
sys 0m1.068s
package main
import (
"fmt"
"io/ioutil"
"path/filepath"
"strings"
)
func main() {
fmt.Println(dirwalk("/"))
}
func dirwalk(dir string) []string {
files, err := ioutil.ReadDir(dir)
if err != nil {
fmt.Println(err)
}
var paths []string
for _, file := range files {
if file.IsDir() {
paths = append(paths, dirwalk(filepath.Join(dir, file.Name()))...)
continue
}
if strings.Contains(file.Name(), "README"){
paths = append(paths, filepath.Join(dir, file.Name()))
}
}
return paths
}
#普通のfind
$ time find / -name "*README*"
real 0m1.273s
user 0m0.516s
sys 0m0.680s
#goで書いたfind
$ time ./cmd
real 0m2.167s
user 0m1.000s
sys 0m1.184s
#普通のfind
$ time find / -name "*README*" | wc -l
3354
real 0m1.273s
user 0m0.516s
sys 0m0.680s
#goで書いたfind
$ time ./cmd / README | wc -l
3361
real 0m1.648s
user 0m0.952s
sys 0m1.240s
#普通のfind
$ time find / -name "*README*" | wc -l
416
real 0m0.921s
user 0m0.236s
sys 0m0.308s
#goで書いたfind
$ time ./cmd / README | wc -l
419
real 0m0.595s
user 0m0.572s
sys 0m0.536s
#普通のfind
0時58分14秒 CPU %usr %nice %sys %iowait %irq %soft %steal %guest %gnice %idle
10時58分15秒 all 5.53 0.00 9.55 0.00 0.00 0.17 0.00 0.00 0.00 84.76
10時58分15秒 0 3.03 0.00 10.10 0.00 0.00 1.01 0.00 0.00 0.00 85.86
10時58分15秒 1 14.85 0.00 27.72 0.00 0.00 0.99 0.00 0.00 0.00 56.44
10時58分15秒 2 15.46 0.00 18.56 0.00 0.00 0.00 0.00 0.00 0.00 65.98
10時58分15秒 3 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 100.00
10時58分15秒 4 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 100.00
10時58分15秒 5 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 100.00
#goで書いたfind
10時58分42秒 CPU %usr %nice %sys %iowait %irq %soft %steal %guest %gnice %idle
10時58分43秒 all 10.89 0.00 15.58 0.00 0.00 0.00 0.00 0.00 0.00 73.53
10時58分43秒 0 12.00 0.00 14.00 0.00 0.00 0.00 0.00 0.00 0.00 74.00
10時58分43秒 1 10.00 0.00 16.00 0.00 0.00 0.00 0.00 0.00 0.00 74.00
10時58分43秒 2 12.00 0.00 17.00 0.00 0.00 0.00 0.00 0.00 0.00 71.00
10時58分43秒 3 9.00 0.00 14.00 0.00 0.00 0.00 0.00 0.00 0.00 77.00
10時58分43秒 4 10.42 0.00 13.54 0.00 0.00 0.00 0.00 0.00 0.00 76.04
10時58分43秒 5 11.88 0.00 18.81 0.00 0.00 0.00 0.00 0.00 0.00 69.31
package fingo
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"runtime"
"strings"
"sync"
"unsafe"
)
func para(root, word string, d os.FileInfo, buff *[]byte, wg *sync.WaitGroup) {
*buff = append(*buff, dirwalk(word, filepath.Join(root, d.Name()))...)
defer wg.Done()
}
func FindFile(root, word string) string {
dir, err := ioutil.ReadDir(root)
if err != nil {
fmt.Println(err)
}
runtime.GOMAXPROCS(runtime.NumCPU())
wg := new(sync.WaitGroup)
buff := make([]byte, 0, 1000)
for _, d := range dir {
if !d.IsDir() {
dirwalk(word, filepath.Join(root, d.Name()))
continue
}
wg.Add(1)
go para(root, word, d, &buff, wg)
}
wg.Wait()
return string(buff)
}
func dirwalk(word, dir string) []byte {
files, err := ioutil.ReadDir(dir)
if err != nil {
fmt.Println(err)
}
paths := make([]byte, 0, 200)
for _, file := range files {
if file.IsDir() {
paths = append(paths, dirwalk(word, filepath.Join(dir, file.Name()))...)
continue
}
if strings.Contains(file.Name(), word) {
path := filepath.Join(dir, file.Name()) + "\n"
bp := *(*[]byte)(unsafe.Pointer(&path))
paths = append(paths, bp...)
}
}
return paths
}
package main
import (
"fmt"
"github.com/nao4arale/fingo"
"os"
"runtime"
)
func main() {
/* os.Args[1]...Dirctory, os.Args[2]...To find Words. */
fmt.Printf("%s", fingo.FindFile(os.Args[1], os.Args[2]))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment