[GO] Speed up C / C ++ compilation with ccache

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.

What is ccache

ccache is a tool that caches C / C ++ compile-time data to speed up the second and subsequent compilations.

Introduction

Easy to install with various package managers

For Mac

$ brew install ccache

For Debian

$ sudo apt install ccache

Basic usage

Just put the ccache command in front of the compiler you use all the time

Use with gcc

$ ccache gcc sample.c

Use with clang

$ ccache clang sample.c

Used with g ++

$ ccache g++ sample.cpp

Used with clang ++

$ ccache clang++ sample.cpp

Try using it to compile Go language

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.

Make a call file

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

Try to compile

$ CC=ccache-gcc go build sample.go

Verification

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.

Compile with gcc (without ccache)

$ CC=gcc time go build sample.go

real	0m42.89s
user	0m39.95s
sys 	0m2.14s

Compile with clang (without ccache)

$ CC=clang time go build sample.go

real	0m36.53s
user	0m32.93s
sys 	0m1.75s

Compile with gcc with ccache

$ CC=ccache-gcc time go build sample.go

real	0m4.143s
user	0m2.942s
sys 	0m1.194s

Compile with clang with ccache

$ CC=ccache-clang time go build sample.go

real	0m3.80s
user	0m2.10s
sys 	0m1.24s

Summary

You can be happy if you install ccache and write ʻexport CC = ccache-gcc` in bashrc.

Recommended Posts

Speed up C / C ++ compilation with ccache
Roughly speed up Python with numba
Container-like # 1 made with C
Container-like # 2 made with C
Memo of "Cython-Speeding up Python by fusing with C"
Wrap C/C ++ with SWIG to speed up Python processing. [Overview]
BASIC and C and assembler speed comparison and optimization play with IchigoJam
Using X11 with ubuntu18.04 (C)
ABC163 C problem with python3
Python, Java, C ++ speed comparison
Try Google Mock with C
Python compilation with pyqt deploy
Speed up the netstat command
Format C source with pycparser
Easy Python compilation with NUITKA-Utilities
ABC187 C problem with python
Don't write Python if you want to speed it up with Python
[Python] Hit Keras from TensorFlow and TensorFlow from c ++ to speed up execution
Speed up processing by using C ++ vector, unordered_map internally in Cython