简体   繁体   English

变量初始化/赋值语句是单个操作还是多个操作?

[英]Is variable initialization/assignment statement is a single operation or more?

i'm just learning about algorithm's performance analysis我只是在学习算法的性能分析

my question :is int i=0;我的问题:是int i=0; is counted as two operation: assisgment ,intialization .or it's only one compact operation?被算作两个操作:辅助,初始化。或者它只是一个紧凑的操作? i'm using asymptotic analysis which uses the no of operations as a an example of how to describe the performance我正在使用渐近分析,它使用操作数作为如何描述性能的示例

There is not one correct answer to this.对此没有一个正确的答案。

In some contexts, people may analyze a single arithmetic operation as being "one" operation.在某些情况下,人们可能会将单个算术运算分析为“一个”运算。 Other times, they may care a lot exactly how many cycles an operation will take, and they may want to say that eg adding ints is cheaper than multiplying floats.其他时候,他们可能非常关心一个操作需要多少个周期,他们可能想说,例如,添加整数比乘以浮点数更便宜。

In many contexts, programmers say that dereferencing a pointer is O(1) .在许多情况下,程序员说取消引用指针是O(1) However, if you really are considering the case when n goes to infinity, if your n is like the size of the universe, does it still make sense to say that dereferencing a pointer into that is O(1) operation?然而,如果你真的在考虑n趋于无穷大的情况,如果你的n就像宇宙的大小,那么说将一个指针解引用到那个是O(1)操作仍然有意义吗? In some theoretical contexts , people will say that dereferencing a pointer is O(log n) essentially.在某些理论背景下,人们会说取消引用指针本质上是O(log n)

However, I talked to a physicist once who said that even O(log n) possibly is not physically realistic -- he says there is a maximum information density that the universe can have in a given volume space.然而,我曾与一位物理学家交谈过,他说即使是O(log n)也可能在物理上是不现实的——他说宇宙在给定的体积空间中可以具有最大的信息密度。 This is called the " Bekenstein Bound ", it turns out that the maximum entropy is achieved by a black hole.这被称为“ Bekenstein Bound ”,事实证明最大熵是由黑洞实现的。 If you assume that in a volume of space n you can only have O(n) bits of memory, it basically means that in a galactic-sized computer, bits have to be packed like balls in the sphere packing problem , and then the distance between a typical pair of bits is growing as O(n^{1/3}) .如果你假设在一个空间n你只能有O(n)位的内存,这基本上意味着在一个银河大小的计算机中,位必须像球体包装问题中的一样被包装,然后距离典型的一对位之间的增长为O(n^{1/3}) Since information doesn't go faster than speed of light... maybe dereferencing a pointer needs O(n^{1/3}) asymptotically?由于信息不会比光速更快……也许解引用指针需要O(n^{1/3})渐近? Oo

Point is, don't overthink this.重点是,不要想太多。 Any thing that you do in asymptotic analysis is ultimately an approximation, you are just trying to get a useful idea of how long it will take.你在渐近分析中所做的任何事情最终都是一个近似值,你只是想知道它需要多长时间。 Things that you are pretty sure won't really matter in your case, you can analyze as O(1) , in order to focus on the things that do matter.您非常确定在您的情况下并不重要的事情,您可以分析为O(1) ,以便专注于重要的事情。 In another situation, those same things you may want to analyze differently, if they represent "most" of the work.在另一种情况下,如果这些相同的事情代表了工作的“大部分”,那么您可能希望对其进行不同的分析。

IMO if you want to know how to answer questions about this stuff in a job interview, you should be prepared to analyze it in multiple different ways, and show them that you understand that things like "does int i = 0; count as one operation or two" are basically a convention. IMO 如果您想知道如何在求职面试中回答有关这些问题的问题,您应该准备好以多种不同的方式对其进行分析,并向他们表明您了解诸如“does int i = 0; count as an operation”之类的事情或两个”基本上是一个约定。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM