donglian1953 2013-12-27 03:49
浏览 41
已采纳

如何在golang中正确使用judy array lib?

In golang, the way calling C library is different from what's used in other mainframe dynamic language like PHP / Python / Java because Golang has a different multitasking mechanism which is not OS thread based, so call c function may result in a context switching or thread switching as I understand. In my project I'm trying to use Judy Array in Golang (as a queue worker) to do some simple but large amount dict-related calculation like "select distinct", so

What's the best practice to involve such c lib (for relatively high density calculation) and minimalise the performance overhead introduced as much as possible?

  • 写回答

2条回答 默认 最新

  • drr7731 2013-12-30 22:10
    关注

    Despite the title, the question here really has two parts: a generic one about golang and C-interfacing for efficiency, and a specific one about performant use of judy arrays.

    This thread seems to summarize the costs: https://groups.google.com/forum/#!topic/golang-nuts/RTtMsgZi88Q , so yeah its expensive compared to straight C, and you should try to minimize the crossover points from Go to C.

    Here's additional, judy array specific advice: I've used judy arrays before in C/C++ code. The library's interface is not intuitive in certain places. And by default it uses a C-macro based API, which makes it tricky to get the interface usage correct because the compiler can't offer as much help as usual.

    What I recommend, therefore, is that you write your tests and benchmarks in C first, so you understand the API and its weird cases. Judy arrays when benchmarked for my application (vs C++ vector of strings) were 3x faster, so it can be worth it. But break the task into three phases. First do what you want to do in C, and make sure it works as expected in your own C code. Then expand the basic C interface to handle batches of what you need done, so as to minimize the number of Go->C switches. Then bind your new C interface from Go.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测