我搜索一个复制文件(二进制或文本)的好方法.我写过几个样本,每个人都在工作.但我想听听经验丰富的程序员的意见.
我错过了很好的例子并搜索了一种适用于C++的方法.
ANSI-C-WAY
#include <iostream>
#include <cstdio> // fopen, fclose, fread, fwrite, BUFSIZ
#include <ctime>
using namespace std;
int main() {
clock_t start, end;
start = clock();
// BUFSIZE default is 8192 bytes
// BUFSIZE of 1 means one chareter at time
// good values should fit to blocksize, like 1024 or 4096
// higher values reduce number of system calls
// size_t BUFFER_SIZE = 4096;
char buf[BUFSIZ];
size_t size;
FILE* source = fopen("from.ogv", "rb");
FILE* dest = fopen("to.ogv", "wb"); …Run Code Online (Sandbox Code Playgroud) 我可以根据名称进行猜测,但具体是UNIX中的wall-clock-time,user-cpu-time和system-cpu-time?
user-cpu time是执行用户代码所花费的时间,而kernel-cpu time是由于需要特权操作(如IO到磁盘)而花费在内核中的时间吗?
这个测量的时间单位是.
挂钟时间真的是进程在CPU上花费的秒数,还是名称只是误导?
在UNIX/LINUX中,是否有一种简单的方法来跟踪命令所需的时间?
我正在学习如何使用Python中threading的multiprocessing模块和并行运行某些操作并加快我的代码.
我发现这很难(可能因为我没有任何理论背景)来理解一个threading.Thread()对象和一个对象之间的区别multiprocessing.Process().
此外,我并不完全清楚如何实例化一个作业队列,并且只有4个(例如)它们并行运行,而另一个则在执行之前等待资源释放.
我发现文档中的示例清晰,但不是很详尽; 一旦我尝试使事情复杂化,我就会收到许多奇怪的错误(比如一种无法腌制的方法,等等).
那么,我什么时候应该使用threading和multiprocessing模块?
您能否将我链接到一些资源,解释这两个模块背后的概念以及如何正确使用它们来完成复杂的任务?
python parallel-processing multithreading process multiprocessing
我试图重定向时间命令的输出,但我不能:
$time ls > filename
real 0m0.000s
user 0m0.000s
sys 0m0.000s
Run Code Online (Sandbox Code Playgroud)
在文件中,我可以看到ls命令的输出,而不是time.请解释一下,为什么我不能以及如何做到这一点.
我刚刚发现我的脚本给了我一个致命的错误:
Fatal error: Allowed memory size of 268435456 bytes exhausted (tried to allocate 440 bytes) in C:\process_txt.php on line 109
Run Code Online (Sandbox Code Playgroud)
那条线是这样的:
$lines = count(file($path)) - 1;
Run Code Online (Sandbox Code Playgroud)
所以我认为将文件加载到记忆中并计算行数很困难,是否有更有效的方法可以做到这一点而不会出现内存问题?
我需要计算的行数为2MB到500MB的文本文件.也许有时候是Gig.
谢谢大家的帮助.
我试图找出我的代码的性能,但我不明白time命令的输出,任何人都可以解释时间命令输出的含义.
以下是我得到的:
time ./filereader
real 0m0.193s
user 0m0.012s
sys 0m0.056s
Run Code Online (Sandbox Code Playgroud)
什么是real,user,sys?
$ time ./Test
real 0m2.906s
user 0m2.887s
sys 0m0.017s
Run Code Online (Sandbox Code Playgroud)
这是程序代码:
#include <iostream>
#include <map>
void func_a() {
std::map<int, int> m;
for (unsigned int i = 0; i < 10000; i++) {
m.insert(std::pair<int, int>(i, i));
}
}
void func_b() {
std::map<int, int> m;
for (unsigned int i = 0; i < 1000000; i++) {
m.insert(std::pair<int, int>(i, i));
}
}
int main() {
func_a();
func_b();
return 0;
}
Run Code Online (Sandbox Code Playgroud) unix ×4
linux ×2
python ×2
time ×2
c++ ×1
command-line ×1
file ×1
file-io ×1
memory ×1
memory-leaks ×1
optimization ×1
php ×1
process ×1
shell ×1
terminal ×1
terminology ×1
text ×1