语言与文风2022espadrine.github.io
有时,用另一种语言重写会奏效
Sometimes, rewriting in another language works
导读
作者把一段程序用另一种语言重写,结果性能大幅提升,并以此反思“换语言重写”这件事。评论区补充了关键细节:Julia 版本慢只因一行多余的字符列表构造,Rust 版本最初还忘了开优化编译。对写作者来说,这篇值得读的地方在于:它示范了“换一种表达方式重写”如何暴露原稿里隐藏的冗余,以及下结论前先确认前提条件。
可以直接用的写作要点
- 01
重写能暴露原稿中看不见的冗余:先找出那句“每次调用都重复做一遍”的多余动作,删掉它往往比换工具更有效。
- 02
判断一种表达方式好不好,要看它是否让人容易做对:留意自己写作流程中的“默认设置”,它可能在悄悄拖慢你。
- 03
下结论前先核对前提条件,就像那位作者忘了开优化编译一样,很多“写法 A 不如写法 B”的结论其实是测量条件不对。
- 04
引用格言要看清适用范围:“永不重写”针对的是大型系统重写,而不是两小时的小规模改写,类比到写作上就是分清“改稿”和“推倒重来”。
HN 评论精选
先确认测量条件(是否开优化),再评价语言;好的语言设计应让人容易做对。
有趣的是,作者对“只花 7 小时”这个结果挺满意,后来才发现自己编译 Rust 时忘了开优化,运行时间因此降到 30 分钟。我没用过 Julia,但未优化的 Rust 二进制生成的代码非常朴素低效,所以我猜他们的 Julia 代码本身一定有问题;我估计未优化 Rust 的性能大致相当于解释执行的(非 JIT)Ruby 或 Python。不过我们也可以从“一种语言是否让人容易掉进成功陷阱而非失败陷阱”来评价它:如果他们的 Julia 代码确实有问题,也许 Julia 开发者可以调整一些默认设置或改文档,让后来者不再踩同样的坑。Rust 也有类似情况:I/O 默认不缓冲,导致在循环里疯狂打印的微基准测试表现很差。
kibwen查看英文原文
I'm amused that the author reached a satisfactory result of "only" 7 hours, just to later realize that they forgot to compile their Rust code with optimizations thus bringing the runtime down to 30 minutes. I've never used Julia, but unoptimized Rust binaries contain such naive and suboptimal codegen that I assume that something must have been wrong with their Julia code; my ballpark for the performance of unoptimized Rust code is that it's roughly on-par with interpreted (not JITted) Ruby or Python (parallelism notwithstanding, which even without optimizations bears much more fruit in Rust than in Ruby/Python). Still, at the same time, we can judge a language based on how easy it is to fall into the pit of success rather than the pit of failure; if their Julia code had a problem, then maybe the Julia developers could tweak some defaults (or change some documentation) so that people in the future do not fall afoul of the same result. The same sort of thing exists in Rust as well, where the fact that I/O is unbuffered by default makes it perform poorly in microbenchmarks that involve printing a zillion times in a loop: https://nnethercote.github.io/perf-book/io.html#buffering
性能差距常来自一行多余的重复转换,而非语言本身。
Julia 版本的问题只是每次调用 match-constraint 时都把单词收集成一个字符列表。删掉那一行,Julia 版本就快了 5 倍。Rust 版本做的也正是这件事。
adgjlsfhk1查看英文原文
The problem with the Julia version is just that it collects a word to a list of characters every time it calls match-constraint. Removing that one line makes the Julia version 5x faster. function match_constraint(word::String, constraint::Constraint)::Bool if constraint.type == has_no constraint.letter ∉ word elseif constraint.type == has_but_not_at (constraint.letter ∈ word) && (word[constraint.index] != constraint.letter) elseif constraint.type == has_at word[constraint.index] == constraint.letter end end This is also what the rust version does.
“永不重写”针对的是大型重写,小规模改写不适用这条格言。
“我觉得两小时就能用 Rust 重写出来。”结合这篇文章的标题,我不确定该得出什么结论。重写往往能带来很大改善,但两小时的时间投入,并不是人们重复“永不重写”这类格言时所指的情况。
trimbo查看英文原文
> I felt I could rewrite it in Rust within two hours. I'm not sure what to conclude given the title of this post. Rewrites often improve things greatly. But a 2 hour time investment is not what anyone is talking about when they repeat mottos like "never rewrite."
同类还值得读
像说话一样写作
Write like you talk
这篇短文主张写作应像说话一样自然、清晰,避免装腔作势的书面腔。
语言与文风2015 · 647 分 · 272 评4 条写作要点编辑精选法律语言难懂源于写作不佳,而非专业概念
Poor writing, not specialized concepts, drives difficulty with legal language
这是一项认知科学研究:法律文本难懂,主要不是因为充斥专业术语,而是因为写作者把句子写得又长又绕、修饰语层层嵌套、回指距离过远。
语言与文风2022 · 559 分 · 322 评5 条写作要点编辑精选写得简单
Write Simply
保罗·格拉汉姆在这篇短文里主张:好文章应该写得简单——用日常词汇、短句、直白的结构,不为显得聪明而堆砌复杂表达。
语言与文风2021 · 484 分 · 330 评4 条写作要点编辑精选Tortured phrases:科学写作中出现的一种可疑文风
Tortured phrases: A dubious writing style emerging in science
《自然》新闻报道了一种在科学论文中蔓延的怪现象:原本标准的术语被同义替换软件改写成了古怪说法,比如把 "cloud computing" 写成 "haze figuring"。
语言与文风2021 · 329 分 · 256 评4 条写作要点编辑精选乔治·奥威尔 1940 年对《我的奋斗》的书评
George Orwell's review of Mein Kampf (1940)
这是奥威尔1940年对《我的奋斗》的书评。
语言与文风2014 · 305 分 · 240 评4 条写作要点编辑精选