我想比较使用Python和C++从stdin读取字符串的读取行,并且看到我的C++代码运行速度比等效的Python代码慢一个数量级,这让我很震惊.由于我的C++生锈了,我还不是专家Pythonista,请告诉我,如果我做错了什么或者我是否误解了什么.
(TLDR回答:包括声明:cin.sync_with_stdio(false)或者只是fgets改用.
TLDR结果:一直向下滚动到我的问题的底部并查看表格.)
C++代码:
#include <iostream>
#include <time.h>
using namespace std;
int main() {
string input_line;
long line_count = 0;
time_t start = time(NULL);
int sec;
int lps;
while (cin) {
getline(cin, input_line);
if (!cin.eof())
line_count++;
};
sec = (int) time(NULL) - start;
cerr << "Read " << line_count << " lines in " << sec << " seconds.";
if (sec > 0) {
lps = line_count / sec;
cerr << " LPS: " << lps …Run Code Online (Sandbox Code Playgroud) 我正在为Web应用程序编写一个日志文件查看器,为此我想通过日志文件的行分页.文件中的项目是基于行的,底部是最新项目.
所以我需要一种tail()方法,可以n从底部读取行并支持偏移量.我想出的是这样的:
def tail(f, n, offset=0):
"""Reads a n lines from f with an offset of offset lines."""
avg_line_length = 74
to_read = n + offset
while 1:
try:
f.seek(-(avg_line_length * to_read), 2)
except IOError:
# woops. apparently file is smaller than what we want
# to step back, go to the beginning instead
f.seek(0)
pos = f.tell()
lines = f.read().splitlines()
if len(lines) >= to_read or pos == 0:
return lines[-to_read:offset and -offset or None]
avg_line_length …Run Code Online (Sandbox Code Playgroud) 在我的工作场所,我们编写了一个自定义log4j appender,它将日志消息写入数据库(异步使用专用线程,因此没有性能损失).我更喜欢写入日志文件 - 基于数据库的日志更容易查询和分析.
是否有一个开源解决方案来执行此操作(特别是对于log4j或任何其他java记录器)?
我们的appender有一些东西,我希望在另一种方法中看到:
我们的appender支持以下列,我希望在我们找到的任何解决方案中看到所有这些列.
文本文件包含两列 - 索引号(5个空格)和字符(30个空格).它按字典顺序排列.我想执行二进制搜索来搜索关键字.