Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
319 views
in Technique[技术] by (71.8m points)

list - 算法列表何时比地图更快?(When are lists algorithmically faster than maps?)

I think this is a valid question because if you use a Map with integers as keys you have a similar structure as a list.

(我认为这是一个有效的问题,因为如果您使用带整数作为键的Map,则其结构类似于列表。)

You can read elements in order with a for loop:

(您可以使用for循环按顺序读取元素:)

for i in 1,..., map.length():
    if i in map:
        doSomething(map[i])

Besides, inserting in list and reading an element is O(n) while inserting and reading in a map is O(1).

(此外,在列表中插入和读取元素为O(n),在地图中插入和读取为O(1)。)

In what case are lists faster than maps?

(在什么情况下列表比地图更快?)

If we are concerned about how fast the code runs, in what cases are the lists not strictly worse than maps?

(如果我们担心代码的运行速度,那么在什么情况下列表严格不比映射差?)

Should we always use maps in that case?

(在这种情况下,我们应该始终使用地图吗?)

Wouldn't a list implemented with a map be a better list?

(用地图实现的列表不是更好的列表吗?)

  ask by Thomas G. translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...