What is a quadratic process?

huangapple go评论86阅读模式
英文:

What is a quadratic process?

问题

我正在阅读《Go编程语言》这本书(作者是Donovan和Kernighan),关于他们的示例echo1,他们说:“这是一个二次过程,如果参数的数量很大,可能会很昂贵,但对于echo来说,这是不太可能的”。什么是二次过程?如果参数的数量很大,为什么会很昂贵?

谢谢。

英文:

I was reading the Go programming language book (by Donovan and Kernighan) and about their example echo1, they said: "This is a quadratic process that could be costly if the number of arguments is large, but for echo, that's unlikely". What does quadratic process mean?, and how is it costly if the number of arguments is large?

Thanks.

答案1

得分: 10

一般来说,二次方指的是与平方数相关的事物。在这个上下文中,它意味着过程的成本与输入大小的平方成比例。这是因为在Go语言中,字符串是不可变的,每次连接字符串时都需要使用+=运算符,这是一种昂贵的操作,因为每次连接都需要在内存中创建一个新的字符串。更高效的字符串连接方法包括将内容写入bytes.Buffer并将其转换为字符串,或者使用strings.Join函数。

英文:

Generally speaking quadratic means something that pertains to squared numbers. In this context it means that the cost of the process is proportional to the square of the input size. This is because strings are concatenated using += operator which is expensive in Go as strings are immutable and a new string must be created in memory every time you concatenate. More efficient ways to concatenate strings include writing to bytes.Buffer and converting it to string, or using strings.Join function

huangapple
  • 本文由 发表于 2017年1月18日 03:12:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/41705138.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定