当前位置:网站首页>Some skills to reduce the complexity of program space
Some skills to reduce the complexity of program space
2022-07-25 09:45:00 【Frost Creek】
For programs , The important criteria to measure its quality are time complexity and space complexity . On the premise that the target task can be completed , Making the program have low time complexity and low space complexity is the goal we have been pursuing .
However, sometimes you can't have both . With less time complexity , Space complexity tends to increase , While reducing the space complexity , Time complexity tends to increase . Want to get a balance between the two , obviously , It's not easy , It can be said that you can't have both fish and bear's paw !
ok , Let's get down to business !
Suppose we imagine such a scenario , There is a file on our computer , The name of the fruit is saved on it , For example, some data like this , Apple , pear , Peach , watermelon , grapes , Apple , watermelon ....... Our goal is to count which of these names appears most often .
The program implementation idea is very simple , That is to initialize a dictionary , If the name does not exist , Then add the name to the dictionary , Its value is set to one , If the name already exists , Then its value is increased by one .
Program :
fruitData={}with open('data.txt') as f:content=f.read().splitlines() # Read entire filefor fruit in content:if fruit in fruitData:fruitData[fruit]=fruitData[fruit]+1else:fruitData[fruit]=1print(fruitData)
ok , It's that simple , But when you think about it, there is a problem , That is, this is to read all the file information into memory . When the file size is small , It doesn't take much memory , Once the scale is large , such as 1000GB( Suppose it exceeds the memory of the computer used ), Then there's a problem .
Before that, read all the data at once , in fact , We can read Enter one Data , Processing a data , When processing the next data , The memory occupied by the previous data will also be released in time , In this way, the problem of too much memory is solved .
Improvement of the original program :
fruitData={}with open('data.txt') as f:for fruit in f: # Read one line at a timefruit=fruit.strip() # Delete the line break at the endif fruit in fruitData:fruitData[fruit]=fruitData[fruit]+1else:fruitData[fruit]=1print(fruitData)
here , The space complexity has also changed from the original O(n) Down to O(1).
边栏推荐
猜你喜欢
随机推荐
Flutter Rive 多状态例子
Learning new technology language process
OC -- Foundation -- string + date and time
Create personal extreme writing process - reprint
How to obtain location information (longitude and latitude) by uni app
初识Opencv4.X----图像模板匹配
基于stm32的恒功率无线充电
matlab绘图|坐标轴axis的一些常用设置
SurfaceView 闪屏(黑一下问题)
MinkowskiEngine 安装
[code source] daily one question non decreasing 01 sequence
Job 7.15 shell script
如何安装pytorch?—— 一种最简单有效的方法!
STM32+HC05串口蓝牙设计简易的蓝牙音箱
初识Opencv4.X----为图像添加高斯噪声
[code source] daily one question three paragraph
【数据挖掘】最近邻和贝叶斯分类器
OC -- first acquaintance
OC -- Foundation -- Collection
OC -- Foundation -- dictionary








