当前位置:网站首页>Time complexity and space complexity
Time complexity and space complexity
2022-07-27 19:33:00 【Brother rabbit head】
1、 Algorithm time complexity and space complexity
1、 Preface
Algorithm (Algorithm) It is used to operate data 、 A set of solutions to procedural problems . For the same question , Using different algorithms , Maybe the end result is the same , But the resources and time consumed in the process will be very different .
So how should we measure the advantages and disadvantages of different algorithms ?
Mainly from the occupied by the algorithm 「 Time 」 and 「 Space 」 Two dimensions to consider .
- Time dimension : The time spent executing the current algorithm , We usually use [ Time complexity ] To describe .
- Spatial dimension : It refers to the memory space required to execute the current algorithm , We usually use [ Spatial complexity ] To describe .
therefore , The efficiency of an algorithm mainly depends on its time complexity and space complexity . However , Sometimes time and space are 「 Fish and bear paw 」, Not both , Then we need to strike a balance .
Let me introduce them separately 「 Time complexity 」 and 「 Spatial complexity 」 Calculation method of .
Two 、 Time complexity
We want to know the of an algorithm 「 Time complexity 」, The first way many people think of is to run the algorithm program again , Then the time it takes will naturally know .
Is this way OK ? Certainly. , But it also has many disadvantages .
This approach is very vulnerable to the operating environment , The results of running on high-performance machines will be very different from those on low-performance machines . And it also has a great relationship with the size of the data used in the test . also , And when we write algorithms , There is no way to run it completely .
therefore , Another more general method came out :「 Big O Symbolic representation 」, namely T(n) = O(f(n))
Let's start with an example :
for(i=1; i<=n; ++i)
{
j = i;
j++;
}
adopt 「 Big O Symbolic representation 」, The time complexity of this code is :O(n) , Why? ?
At large O In symbolic representation , The formula of time complexity is :T(n) = O( f(n) ), among f(n) Represents the sum of execution times per line of code , and O Represents a positive proportional relationship , The full name of this formula is : Progressive time complexity of the algorithm .
Let's continue with the example above , Suppose that the execution time of each line of code is the same , We use it 1 Particle size is expressed by time , So the first line of this example takes 1 Particle time , The execution time of the third line is n Particle time , The execution time of the fourth line is also n Particle time ( The second and fifth lines are symbols , Temporary neglect ), So the total time is 1 Particle time + n Particle time + n Particle time , namely (1+2n) Particle time , namely :T(n) = (1+2n)* Particle time , From this result, we can see that , This algorithm takes time n Change by change , therefore , We can simply express the time complexity of this algorithm as :T(n) = O(n)
边栏推荐
猜你喜欢

阿里云对象存储OSS的开通和使用

kettle8.2 安装及常见问题

kettle 合并记录 数据减少

2022 Ningde Vocational College Teachers' practical teaching ability improvement training - network construction and management

Kettle consolidated record data reduction

c语言:c语言代码风格

Opening and using Alibaba cloud object storage OSS

C language case: password setting and login > clear solution getchar and scanf

200 lines of code quick start document database monogodb

An article allows you to master threads and thread pools, and also solves thread safety problems. Are you sure you want to take a look?
随机推荐
Nacos cluster deployment - high availability guarantee
c语言:12、gdb工具调试c程序
Daily question (02): inverted string
How can I get started quickly when I change my career to soft testing and job hopping to a new company?
mysql学习笔记(1)——变量
kettle JVM内存设置---效果不明显
IPFs obtains the public key and private key through the interface, and encrypts the storage. First bullet
Basic network faults and troubleshooting
webservice的疑问
c语言:8、makeFile编写
一个经验
Kettle JVM memory setting - the effect is not obvious
汉字查拼音微信小程序项目源码
Yanghui triangle
Can set be used to define local variables in OPDS SQL
Using MATLAB to generate graphics for journals and conferences - plot
MFC高级控件之Tab控件( CTabCtrl )
Anaconda下安装Talib库
让你的聊天气泡丰富多彩
Definition of graph traversal and depth first search and breadth first search (2)