内置python函数的时间/空间复杂度

编程入门 行业动态 更新时间:2024-10-11 01:11:31
本文介绍了内置python函数的时间/空间复杂度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

split/strip/open(内置python函数)的时间/空间复杂度是多少?

What is the time/space complexity of split/strip/open (in-built python functions)?

有人知道我可以在哪里查询这些功能的时间/空间复杂性吗?

Does anyone know where i can look up on the time/space complexity of these functions?

推荐答案

确切的答案取决于将哪些属性输入到函数中.最简单的查找方法可能是检查这些功能的源代码.可在此处找到python源代码.

The exact answer will depend on what properties are fed into the function. The easiest way to find out would probably be to examine the source code for those functions. The python source code can be found here.

让我们看看 split的来源.代码根据属性运行不同的循环.这是按空格分割的循环.

Lets take a look at the source for split. The code runs different loops depending on the properties. This is the loop for splitting by whitespace.

while (maxcount-- > 0) { while (i < str_len && STRINGLIB_ISSPACE(str[i])) i++; if (i == str_len) break; j = i; i++; while (i < str_len && !STRINGLIB_ISSPACE(str[i])) i++;

在此代码中,该函数将查看字符串中的每个字符(除非达到了最大计数).对于大小为n的字符串,最里面的循环将运行n次.时间复杂度为 O(n)

In this code, the function will look at each character in the string (unless the maxcount is reached). The inner most loops will run n times for a string of size n. Time complexity is O(n)

地带来源逐步浏览字符串.

i = 0; if (striptype != RIGHTSTRIP) { while (i < len) { Py_UCS4 ch = PyUnicode_READ(kind, data, i); if (!BLOOM(sepmask, ch)) break; if (PyUnicode_FindChar(sepobj, ch, 0, seplen, 1) < 0) break; i++; } } j = len; if (striptype != LEFTSTRIP) { j--; while (j >= i) { Py_UCS4 ch = PyUnicode_READ(kind, data, j); if (!BLOOM(sepmask, ch)) break; if (PyUnicode_FindChar(sepobj, ch, 0, seplen, 1) < 0) break; j--; } j++; }

这使剥离的时间复杂度为 O(n).

This gives strip a time complexity of O(n).

open()的源没有循环.这就是我们所期望的.没有什么可以循环的.

The Source for open() shows no loops. This is what we would expect. There is nothing to loop through.

更多推荐

内置python函数的时间/空间复杂度

本文发布于:2023-11-29 13:49:51,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1646564.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:复杂度   函数   时间   空间   python

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!