Simon Willison 用 Claude 对比 C 与 Rust 实现 parse_number() SQLite 扩展的性能与体积
Simon Willison 在 Claude(Opus 5.5)协助下编写将 "-$1,234.56"、"12.5%" 等字符串转为数值的 parse_number() SQLite 扩展,对比 C 与 Rust 实现。
Simon Willison 博客·2026-11-09 06:26· 2 小时前AI 评分55
parse_number() 作为 SQLite 扩展:C 与 Rust 对比
AI 导读
作者在 Claude(Opus 5.5)协助下编写将 "-$1,234.56"、"12.5%" 等字符串转为数值的 parse_number() SQLite 扩展,对比 C 与 Rust 实现。
正文 · 原文
研究: parse_number() 作为 SQLite 扩展:C 与 Rust 对比
我遛狗时突然好奇,编写一个 SQLite C 扩展来添加 parse_number() 函数,把 -$1,234.56 或 '12.5%' 这样的字符串转换成可求和、可排序的数值,会有多容易或多困难。
我打开了普通的 Claude 聊天(Opus 5.5)并输入了提示:
I'm thinking about a SQLite custom function 'parse_number(value)' that strips ',', '$', '£' and '%', handles a leading '-', and returns 'NULL' for anything it can’t parse
Explore the difference between building this as a SQLite extension in C v.s. in Rust - I want to compare performance and code complexity and size of the resulting.so(or whatever) file
这给了我一个扎实的起点——Rust 扩展为 321 KB(使用 Rusqlite)和 298 KB(使用 sqlite-loadable-rs),而 C 扩展为 14 KB。但 Rust 扩展更快!
原来是因为 Rust 中对应 C 的 strtod 的实现“快了一倍”。于是我提示:
Try writing your own C function for parsing text into a decimal number that catches up to or beats the Rust implementation
它做到了:
200 万行数据下每行的净开销,越低越好:
最初的 C 新的 C rusqlite sqlite-loadable 混合数据 67 ns 19 ns 46 ns 41 ns 仅整数 25 ns 16 ns 36 ns 32 ns 仅小数 106 ns 18 ns 55 ns 47 ns
最后,我让它把原型改写成一份研究报告,以与其他 simonw/research 中的报告保持一致,提示如下:
Clone simonw/research from GitHub and turn your above investigation into a research report as seen in that repository - you can give me a zip file to download that contains your new folder
这是最终生成的报告。
标签:c、research、sqlite、rust、claude
来源:Simon Willison 博客 · simonwillison.net
来源:RadarAI · aihot.news