- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在重构一个可怕的 python 脚本,它是生成 lua 绑定(bind)的 polycode 项目的一部分。
我正在考虑将生成的 lua 行写成 block 。
但我的一般问题是,快速写入文件有哪些不利/注意事项?
举个例子:
persistent_file = open('/tmp/demo.txt')
for i in range(1000000):
persistent_file.write(str(i)*80 + '\n')
for i in range(2000):
persistent_file.write(str(i)*20 + '\n')
for i in range(1000000):
persistent_file.write(str(i)*100 + '\n')
persistent_file.close()
这只是一种尽可能快地向文件写入大量内容的简单方法。我真的不希望这样做会遇到任何实际问题,但我确实想知道,为一次大写入缓存是否有利?
最佳答案
来自open
函数的文档:
open(file, mode='r', buffering=-1, encoding=None, errors=None, newline=None, closefd=True, opener=None) -> file object
...
buffering is an optional integer used to set the buffering policy. Pass 0 to switch buffering off (only allowed in binary mode), 1 to select line buffering (only usable in text mode), and an integer > 1 to indicate the size of a fixed-size chunk buffer. When no buffering argument is given, the default buffering policy works as follows:
Binary files are buffered in fixed-size chunks; the size of the buffer is chosen using a heuristic trying to determine the underlying device's "block size" and falling back on
io.DEFAULT_BUFFER_SIZE
. On many systems, the buffer will typically be 4096 or 8192 bytes long."Interactive" text files (files for which isatty() returns True) use line buffering. Other text files use the policy described above for binary files.
换句话说,在大多数情况下,您在频繁调用 write()
时遇到的唯一开销是函数调用的开销。
关于python - 频繁地在文件对象上调用 write() 并添加少量内容是不是很糟糕?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30688445/
我想在这里说的是我在从之前离开的相同状态重新启动我的应用程序时遇到的问题。我在这方面做了很多研发,并且已经解决了 stackoverflow 中发布的问题。所以请不要说它是重复的。 我试过设置这些选项
当我在我的类中实现 __cmp__ 函数时,python 是否会在内部重载“==”,我们在 C++ 中是如何做到的? 只是好奇。我是 python 的新手。 :) 最佳答案 ==的含义当您定义 __c
我在 Raspberry Pi2 上安装了 Gitlab,几个月来它运行良好。但自从关闭了RPi的电源后,它就不再起作用了。网页返回502错误。 502 Whoops, GitLab is takin
有人知道用户登陆带有Webfonts的页面时为什么Google Chrome浏览器崩溃吗 它并不会一直发生,而是经常发生 我刚得到一个蓝屏页面,却不知道为什么:该页面不是来自重定向时就很好了。 这是我
当我登录时,Skype始终会给出此错误。 糟糕,Skype存在问题。尝试注销然后重新登录。 STARTUP_LOAD_ERROR MACBOOK 最佳答案 Macbook 用户 退出Skype 回家
我正在尝试从 flutter 开始,首先我在 cmd 上运行 flutter doctor 它有效。在我安装了 ANDROID SDK 之后,同样的命令 flutter doctor 给了我异常:
从 android studio 终端运行 flutter attach 不工作。显示错误flutter 意外退出。 终端输出: flutter attach Checking for adverti
当使用 TinyMCE 4 测试所有浏览器时,Chrome 非常慢。 (我尝试从 TinyMCE 中删除所有插件,但没有任何区别。) Chrome 需要大约 20-25 秒在 TinyMCE 中呈现一
我试图让下面的脚本工作,以便从远程服务器(服务器 1)读取特定目录中的 CSV 文件列表,并将数据移植到另一台服务器的 PostgreSQL 数据库中。 我已经创建了一个 rsa SSH key 并将
在嵌入式 linux 环境中(在 PowerPC 上定制的 2.4.25)几个小时后我得到以下内核 panic : Oops: kernel access of bad area, sig: 11 N
在将现有 Node.js (Hapi.js) + RethinkDB 从 OVH VPS(最小 vps)迁移到 AWS Lambda( Node )+ DynamoDB 的过程中,我最近遇到了一个非常
我是一名优秀的程序员,十分优秀!