我需要在python中获取大文件(数十万行)的行数.记忆和时间方面最有效的方法是什么?
目前我这样做:
def file_len(fname):
with open(fname) as f:
for i, l in enumerate(f):
pass
return i + 1
Run Code Online (Sandbox Code Playgroud)
有可能做得更好吗?
我正在尝试将一些代码从Python转换为C++,以便获得一点速度并提高我生锈的C++技能.昨天我感到震惊的是,在Python中,从stdin读取行的简单实现要比C++快得多(参见本文).今天,我终于想出了如何在C++中使用合并分隔符(与python的split()类似的语义)拆分字符串,现在我正在体验似曾相识!我的C++代码需要更长的时间才能完成工作(尽管不是一个数量级,就像昨天的课程一样).
Python代码:
#!/usr/bin/env python
from __future__ import print_function
import time
import sys
count = 0
start_time = time.time()
dummy = None
for line in sys.stdin:
dummy = line.split()
count += 1
delta_sec = int(time.time() - start_time)
print("Python: Saw {0} lines in {1} seconds. ".format(count, delta_sec), end='')
if delta_sec > 0:
lps = int(count/delta_sec)
print(" Crunch Speed: {0}".format(lps))
else:
print('')
Run Code Online (Sandbox Code Playgroud)
C++代码:
#include <iostream>
#include <string>
#include <sstream>
#include <time.h>
#include <vector>
using namespace std;
void split1(vector<string> &tokens, const string …Run Code Online (Sandbox Code Playgroud) 我做了一个测试来比较几种语言的字符串操作,以便为服务器端应用程序选择一种语言.结果似乎很正常,直到我最终尝试了C++,这让我感到非常惊讶.所以我想知道我是否错过了任何优化并来到这里寻求帮助.
测试主要是密集的字符串操作,包括连接和搜索.测试在Ubuntu 11.10 amd64上进行,GCC版本为4.6.1.该机器是戴尔Optiplex 960,配备4G内存和四核CPU.
def test():
x = ""
limit = 102 * 1024
while len(x) < limit:
x += "X"
if x.find("ABCDEFGHIJKLMNOPQRSTUVWXYZ", 0) > 0:
print("Oh my god, this is impossible!")
print("x's length is : %d" % len(x))
test()
Run Code Online (Sandbox Code Playgroud)
结果如下:
x's length is : 104448
real 0m8.799s
user 0m8.769s
sys 0m0.008s
Run Code Online (Sandbox Code Playgroud)
public class test {
public static void main(String[] args) {
int x = 0;
int limit = 102 * 1024;
String s="";
for (; s.length() …Run Code Online (Sandbox Code Playgroud) 我正在听Andrei Alexandrescu关于D编程语言的谷歌谈话,当时他抛出一个关于"endl"惨败的内线.我只是认为endl是表示行结束并刷新流缓冲区的首选方式.为什么它被视为惨败?我不应该在我的代码中使用它吗?
如何在C中获得微秒时间戳?
我正在尝试:
struct timeval tv;
gettimeofday(&tv,NULL);
return tv.tv_usec;
Run Code Online (Sandbox Code Playgroud)
但是这会返回一些无意义的值,如果我得到两个时间戳,第二个可以比第一个更小或更大(第二个应该总是更大).是否可以将gettimeofday返回的魔术整数转换为可以实际使用的正常数字?
我正在研究一个简单的解析器,在进行分析时我发现瓶颈在...文件读取!我摘录了非常简单的测试来比较的性能fstreams和FILE*读取数据的大斑点时:
#include <stdio.h>
#include <chrono>
#include <fstream>
#include <iostream>
#include <functional>
void measure(const std::string& test, std::function<void()> function)
{
auto start_time = std::chrono::high_resolution_clock::now();
function();
auto duration = std::chrono::duration_cast<std::chrono::nanoseconds>(std::chrono::high_resolution_clock::now() - start_time);
std::cout<<test<<" "<<static_cast<double>(duration.count()) * 0.000001<<" ms"<<std::endl;
}
#define BUFFER_SIZE (1024 * 1024 * 1024)
int main(int argc, const char * argv[])
{
auto buffer = new char[BUFFER_SIZE];
memset(buffer, 123, BUFFER_SIZE);
measure("FILE* write", [buffer]()
{
FILE* file = fopen("test_file_write", "wb");
fwrite(buffer, 1, BUFFER_SIZE, file);
fclose(file);
});
measure("FILE* read", [buffer]() …Run Code Online (Sandbox Code Playgroud) 在http://channel9.msdn.com/Events/GoingNative/2013/Writing-Quick-Code-in-Cpp-Quickly的 50:40时,Andrei Alexandrescu开玩笑说如何效率/慢速istream.
我过去遇到过一个问题,ostream很慢,而且fwrite明显更快(在主循环运行一次时减少了很多秒),但我从来不明白为什么也没看过它.
什么使得C++中的istream和ostream变慢?或者至少比其他东西(如fread/fget,fwrite)慢,这同样满足了需求.
我有一个包含数百万行的文件,每行有3个以空格分隔的浮点数.读取文件需要花费大量时间,因此我尝试使用内存映射文件读取它们,但发现问题不在于IO的速度,而在于解析速度.
我当前的解析是获取流(称为文件)并执行以下操作
float x,y,z;
file >> x >> y >> z;
Run Code Online (Sandbox Code Playgroud)
Stack Overflow中的某些人建议使用Boost.Spirit,但我找不到任何简单的教程来解释如何使用它.
我正在尝试找到一种简单有效的方法来解析看起来像这样的行:
"134.32 3545.87 3425"
Run Code Online (Sandbox Code Playgroud)
我真的很感激一些帮助.我想用strtok来分割它,但我不知道如何将字符串转换为浮点数,我不太确定它是最好的方法.
我不介意解决方案是否会提升.我不介意它是不是有史以来最有效的解决方案,但我确信它可以加倍速度.
提前致谢.
我希望在用C++实现的程序的微秒内获得准确的执行时间.我试图用clock_t获得执行时间,但这不准确.
我目前正在学习Boost框架,我已经找到了如何列出我系统上的所有文件夹和文件,使用
#include <boost/filesystem.hpp>
#include <boost/foreach.hpp>
#include <iostream>
using namespace std;
int main()
{
for ( boost::filesystem::recursive_directory_iterator end, dir("C:\\");
dir != end; ++dir ) {
cout << *dir << std::endl;
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
但我遇到的唯一问题是这个过程有多慢......我做错了什么,或者只是微软的.NET版本列出所有文件的速度要快得多?谢谢!
c++ ×8
performance ×4
python ×3
benchmarking ×1
boost ×1
boost-spirit ×1
c ×1
endl ×1
iostream ×1
istream ×1
line-count ×1
node.js ×1
ostream ×1
parsing ×1
split ×1
stl ×1
string ×1
text-files ×1
time ×1