当前位置:网站首页>The numerical value of the number of figures thought of by the real-time update of the ranking list
The numerical value of the number of figures thought of by the real-time update of the ranking list
2022-07-08 01:42:00 【lixia0417mul2】
Regular implementation of leaderboard
1. stand-alone : Use normal values to store the scores of each user , The time complexity of updating a user's score is O(1), The time complexity of querying the ranking list of user scores is O(NlogN), If there is a total of 100000 Users , Yes 1000 Users are updating at the same time , So the time complexity is O(1000) + 1000 * O(100000 * log100000) = 1800000100=1800w operations
2. Distributed : Distribution is generally based on redis Of zset Ordered set implementation , The score of each user is the score of each ordered set element score Value ,redis Of zset The underlying data structure of a set is a jump table , The time complexity of updating scores is O(logN), So use the skip table structure to realize the leaderboard ,10w Users also have 1000 When the user updates , The time complexity is just (1000 * log100000) = 180000=18w operations .
thus it can be seen , If it is stand-alone or distributed, the function of updating the ranking list in real time should be realized , Use ordered arrays ( For example, an ordered array based on the skip table structure ) It can meet the requirements , The time complexity is zero O(logN), Is there another structure that can meet the requirements , And the spatial complexity is only O(N) Well , The answer is yes.
- The tree value is first an array structure , The elements in the array may store the prefixes and , It is also possible to store only its own element values , As for the serial number x The value stored in the element of is the prefix of the previous number of elements and by lowbit(x) The function determines ,lowbit(x) = x & (-x) That is to say x The binary of is 1 The lowest digit of , such as lowbit(6) = lowbit(110) = 2, lowbit(3)=lowbit(011)=1 etc.
- For update operations , Such as update x Fractional value of serial number position , He's not just updating x The element value of position , He will also update those that contain x The value of the sequence number after the prefix value , Suppose there are 16 Array elements , Now you want to update the index location to 6 The element value of , In addition to updates 5 Outside the element value of this position , And update 0b110 + lowbit(110) = 0b1000=8 The value of this position , In addition, continue to update 0b1000+lowbit(0b1000) = 0x10000=16 The value of this position ( However, the value of this position is greater than the maximum array length , No need to update ), It is equivalent to adding lowbit The value of updates the element value of the corresponding index backwards
- For the operation of getting prefix and or interval sum , Because the element value of the current sequence number may not include the element value of all the elements in front of it , So you need to add the previous value , For example, you need to get all the way to the index =6 And , Need to add an index 6 The element value of , Plus 0b110 - lowbit(0b110) = 0b100 The element value of the index , We need to continue to add 0b100-lowbit(0b100) = 0 The element value of the index , This is the complete prefix and
For tree arrays , The time complexity of updating and finding any interval and operation is O(logN), The performance is quite ideal , The scene of ranking can also be transformed into tree array , However, the meaning of storage should be changed as follows , The fraction stored in the array is equal to the number of users of an index value , At this time, when I change my score, such as from oldScore Turned into newScore, Equivalent to index oldScore The number of users minus 1, Follow the previous update process , Then the index is newScore Number of users plus 1, Also follow the previous update process , Finally, I calculate from the index oldScore To newScore And , The sum of this range is the value of the change in the ranking of this user
边栏推荐
- 2022 low voltage electrician examination content and low voltage electrician simulation examination question bank
- The usage of rand function in MATLAB
- 云原生应用开发之 gRPC 入门
- Transportation, new infrastructure and smart highway
- 2022 chemical automation control instrument examination summary and chemical automation control instrument simulation examination questions
- Tapdata 的 2.0 版 ,开源的 Live Data Platform 现已发布
- 从Starfish OS持续对SFO的通缩消耗,长远看SFO的价值
- ROS problems (topic types do not match, topic datatype/md5sum not match, MSG XXX have changed. rerun cmake)
- Anaconda3 tutorial on installing and adding Tsinghua image files
- QT -- package the program -- don't install qt- you can run it directly
猜你喜欢
COMSOL - Construction of micro resistance beam model - final temperature distribution and deformation - establishment of geometric model
城市土地利用分布数据/城市功能区划分布数据/城市poi感兴趣点/植被类型分布
快速熟知XML解析
Understanding of maximum likelihood estimation
Probability distribution
[loss function] entropy / relative entropy / cross entropy
COMSOL----微阻梁模型的搭建---最终的温度分布和变形情况----几何模型的建立
Voice of users | winter goes and spring comes, waiting for flowers to bloom -- on gbase 8A learning comprehension
Gnuradio transmits video and displays it in real time using VLC
2022 high voltage electrician examination skills and high voltage electrician reexamination examination
随机推荐
A little experience from reading "civilization, modernization, value investment and China"
Mysql database (2)
Tencent game client development interview (unity + cocos) double bombing social recruitment 6 rounds of interviews
The difference between distribution function and probability density function of random variables
NPDP在国内有认可度吗?看一看就明白了!
redis的持久化方式-RDB和AOF 两种持久化机制
Anaconda3 download address Tsinghua University open source software mirror station
城市土地利用分布数据/城市功能区划分布数据/城市poi感兴趣点/植被类型分布
COMSOL - Construction of micro resistance beam model - final temperature distribution and deformation - establishment of geometric model
QT build with built-in application framework -- Hello World -- use min GW 32bit
2021 tea master (primary) examination materials and tea master (primary) simulation test questions
2022 free test questions of fusion welding and thermal cutting and summary of fusion welding and thermal cutting examination
Blue Bridge Cup embedded (F103) -1 STM32 clock operation and led operation method
npm 内部拆分模块
AttributeError: ‘str‘ object has no attribute ‘strftime‘
php 获取音频时长等信息
Guojingxin center "APEC education +" Shanghai Jiaotong University Japan Cooperation Center x Fudan philosophy class "Zhe Yi" 2022 New Year greetings
2022 chemical automation control instrument examination summary and chemical automation control instrument simulation examination questions
Frequency probability and Bayesian probability
How does Matplotlib generate multiple pictures in turn & only save these pictures without displaying them in the compiler