Go-sqlite3, which is helpful when dealing with sqlite3 in Go language, but the driver part is written in C language. There is no particular problem with Linux, but compiling on a Mac takes some time, so I investigated whether it would be possible to speed up the compilation.
ccache is a tool that caches C / C ++ compile-time data to speed up the second and subsequent compilations.
Easy to install with various package managers
$ brew install ccache
$ sudo apt install ccache
Just put the ccache
command in front of the compiler you use all the time
$ ccache gcc sample.c
$ ccache clang sample.c
$ ccache g++ sample.cpp
$ ccache clang++ sample.cpp
The C / C ++ compiler used in go build
can be specified by the environment variables $ CC
and $ CXX
.
Using this, try using ccache to compile C / C ++ used in Go language libraries.
It would be nice if you could specify something like CC ='ccache gcc'
, but if you leave it as it is, an error will occur, so create a file for calling.
$ sudo echo '#!/bin/bash
exec ccache gcc $@' > /usr/local/bin/ccache-gcc
$ sudo chmod +x /usr/local/bin/ccache-gcc
$ CC=ccache-gcc go build sample.go
It is a sample that just imports go-sqlite3
package main
import (
"fmt"
_ "github.com/mattn/go-sqlite3"
)
func main() {
fmt.Println("hello, world.")
}
Since it is the second time and later that it is cached and the compilation speeds up, the result of the second time is posted.
$ CC=gcc time go build sample.go
real 0m42.89s
user 0m39.95s
sys 0m2.14s
$ CC=clang time go build sample.go
real 0m36.53s
user 0m32.93s
sys 0m1.75s
$ CC=ccache-gcc time go build sample.go
real 0m4.143s
user 0m2.942s
sys 0m1.194s
$ CC=ccache-clang time go build sample.go
real 0m3.80s
user 0m2.10s
sys 0m1.24s
You can be happy if you install ccache and write ʻexport CC = ccache-gcc` in bashrc.
Recommended Posts