Hi,
Working on an ARM32 environment with ‘slow’ storage media. In trying SQLite on Arm32 I recognized that inserts were so slow that I could notice it simply by looking at it.
I wrote two programs, one in Go, one in C. I compiled both for ARM32,
The C compilation was compiled from the command line with zero additional flags (no optimization).
The Go compilation was tried with and without ARM7 specific compilation, with optimization (go build -ldflags=“-s -w” -o binspeedtest-go .") and without.
The results concern me greatly
C total write time: between 2 and 5 milliseconds
Go total write time: between 116 and 147 milliseconds
Am I doing something wrong, am I missing something? I expect GoLang to use the same system calls as C, with minimal overhead, which is not what I see here.
Please note that I do know the effect of buffering / and that that will improve my results, but that is not what this topic is about. Sadly, even with buffering it does not get close to what I see in C.
file, err := os.Create("golang-optimized.bin")
utils.Debug.Panic(err)
defer file.Close()
buf := make([]byte, 15)
start := time.Now()
for i := 0; i < 1000; i++ {
_, _ = file.Write(buf)
}
end := time.Now()
utils.Print.Ln("Write blank buffer of 15 bytes", end.Sub(start).Milliseconds())
C
typedef struct {
uint8_t age;
char name[50];
uint64_t key;
} SaveToDisk;
#define cycles 1000
long long getMilliTime(void) {
struct timeval tv;
gettimeofday(&tv, NULL);
return ((long long)tv.tv_sec * 1000) + (tv.tv_usec / 1000);
}
int main() {
FILE *fl = fopen(“benchmark-c.bin”, “wb”);
if (fl == NULL) {
perror(“error opening benchmark-c.bin”);
exit(EXIT_FAILURE);
}
SaveToDisk s = {53, “1”, 0x8888888888888888};
long long start = getMilliTime();
for (int n = 0; n < cycles; n++) {
fwrite(&s, sizeof(SaveToDisk), 1, fl);
// sleep(1);
}
long long stop = getMilliTime();
long long duration = stop - start;
float_t avg = (float_t)duration / cycles;
printf(“Start: %lld\nStop: %lld\n”, start, stop);
printf("The program took %lld millis to complete %d cycles at average of %f "
“per write\n”,
duration, cycles, avg);
fclose(fl);
return 0;
}