英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论