gophercon-2018 | Code companion for Gophercon
kandi X-RAY | gophercon-2018 Summary
kandi X-RAY | gophercon-2018 Summary
Code companion for Gophercon 2018
Support
Quality
Security
License
Reuse
Top functions reviewed by kandi - BETA
- Hello is the HTTP response handler
- http server
- driver is used to connect to a request
gophercon-2018 Key Features
gophercon-2018 Examples and Code Snippets
Community Discussions
Trending Discussions on gophercon-2018
QUESTION
I want to write controllers logic and handle json and databases in Golang, while having my math processing model in C. In my opinion overhead calling C function have to be as low, as as setting registers rcx, rdx, rsi, rdi, doin some fastcall and getting out rax value. But i've heard of big overhead in cgo
Say it I have common fastcall x64 c function int64 f(int64 a,b,c,d){return a+b+c+d}
How can I call it from go, to get a highest potential benchmark score in go testing.B
benchmark?
PS no pointer passing, no tricks, just interested in how to access C interface in most robust way
...ANSWER
Answered 2020-Mar-18 at 02:37package main
// #include
// int64_t f(int64_t a, int64_t b, int64_t c, int64_t d) {return a+b+c+d; }
import "C"
import (
"fmt"
"math/rand"
"time"
)
func f(a, b, c, d int64) int64 {
return a + b + c + d
}
func main() {
start := time.Now()
for i := 1; i < 2000; i++ {
a, b, c, d := rand.Int63(), rand.Int63(), rand.Int63(), rand.Int63()
ans_c := int64(C.f((C.int64_t)(a), (C.int64_t)(b), (C.int64_t)(c), (C.int64_t)(d)))
ans_c = ans_c
}
fmt.Printf("cgo time %v\n", time.Since(start))
start = time.Now()
for i := 1; i < 2000; i++ {
a, b, c, d := rand.Int63(), rand.Int63(), rand.Int63(), rand.Int63()
ans_go := f(a, b, c, d)
ans_go = ans_go
}
fmt.Printf("go time %v\n", time.Since(start))
}
Community Discussions, Code Snippets contain sources that include Stack Exchange Network
Vulnerabilities
No vulnerabilities reported
Install gophercon-2018
Support
Reuse Trending Solutions
Find, review, and download reusable Libraries, Code Snippets, Cloud APIs from over 650 million Knowledge Items
Find more librariesStay Updated
Subscribe to our newsletter for trending solutions and developer bootcamps
Share this Page