当前位置:网站首页>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).
边栏推荐
猜你喜欢
随机推荐
初识Opencv4.X----图像模板匹配
微信小程序初步了解及实现底部导航栏
Jar包在阿里云服务器起起来了,安全组也开通了,但postman仍跑不通怎么办
UI - infinite rotation chart and column controller
App的生命周期和AppleDelegate,SceneDelegate
初识Opencv4.X----为图像添加高斯噪声
【数据挖掘】第四章 分类任务(决策树)
SurfaceView 闪屏(黑一下问题)
MinkowskiEngine 安装
About C and OC
Save pdf2image PDF file as JPG nodejs implementation
Assignment 7.21 Joseph Ring problem and decimal conversion
【深度学习】卷积神经网络
[code source] score split of one question per day
Voice chat app source code - produced by NASS network source code
What is cerebral fissure?
*7-2 CCF 2015-09-2 date calculation
Data control language (DCL)
Kotlin realizes file download
How to obtain location information (longitude and latitude) by uni app



![[code source] National Railway](/img/33/ea786a10417487a2426be3a28d28aa.jpg)





