gpt4 book ai didi

c - 为什么 GNU Readline 这么慢?

转载 作者:行者123 更新时间:2023-11-30 17:58:40 25 4
gpt4 key购买 nike

我编写了一个加密程序。

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <readline/readline.h>

int main(void) {
char * plain;
char letter;
int value;
int index;

plain = readline("Please input your plain text: ");
printf("Please input your key (included negatives): ");
scanf("%i", &value);

for (index = 0; index < strlen(plain); index++) {
letter = plain[index];

if (letter >= 'A' && letter <= 'Z') {
fprintf(stderr, "%c", (letter - 'A' + value) % 26 + 'A');
}

else if (letter >= 'a' && letter <= 'z') {
fprintf(stderr, "%c", (letter - 'a' + value) % 26 + 'a');
}
else {
fprintf(stderr, "%c", letter);
}
}
fprintf(stderr, "\n");
free(plain);
}

我做了一些基准测试:

biergaizi@localhost ~/learning_c/test $ time ./caesar_readline < lots_of_letters 2> c_readline_result > /dev/null

real 2m31.212s
user 2m30.776s
sys 0m0.165s

程序花费太多时间从标准输入读取文本。如果我删除 >/dev/null,我可以看到程序正在从标准输入读取,太慢了!

我还写了没有GNU Readline的版本,速度非常快。

为什么?我该如何解决它?

最佳答案

因为它提供的功能。

provides a set of functions for use by applications that allow users to edit command lines as they are typed in. Both Emacs and vi editing modes are available.

我认为你可以只使用开放功能

if (!strcmp(*argv, "-"))
fd = fileno(stdin);
else fd = open(*argv, O_RDONLY|O_NONBLOCK, 0);

如果您想从标准输入读取,请提供 - 作为文件名

关于c - 为什么 GNU Readline 这么慢?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12047925/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com