简体   繁体   English

为什么GNU Readline这么慢?

[英]Why GNU Readline so slow?

I write a program for encrypting. 我写了一个加密程序。

#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);
}

And I did some benchmarks: 我做了一些基准测试:

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

The program spend too much time to read the text from standard input. 该程序花费太多时间从标准输入中读取文本。 If I remove > /dev/null , I can see the program is reading from standard input, too slow! 如果删除> /dev/null ,我可以看到程序正在从标准输入中读取,太慢了!

I also wrote version without GNU Readline, it is very fast. 我也写了没有GNU Readline的版本,它非常快。

Why? 为什么? And How can I solve it? 我该如何解决呢?

Because the feature it provides. 因为它提供了功能。

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. 提供了一组供应用程序使用的功能,这些功能允许用户在键入命令时编辑命令行。Emacs和vi编辑模式均可用。

I think you can just use open function 我认为您可以使用开放功能

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

and provide - as filename if you want read from stdin 并提供-作为文件名,如果您想从标准输入中读取

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM