weixin_36575223 2016-11-01 01:52 采纳率: 0%
浏览 1041

在Linux C++里,用thread让程序更快的运行

我这儿有段码,它可以解一个加密文件的密码,两位数的密码,比如说给一个文档加一个两位数的密码,可以使用这个猜出着两位数的密码。我想问的是怎样使用thread对他稍微改变使他在解码的时候更快速一点。谢谢各位大神!!!

 #include <iostream>
#include <cstdlib>
#include <string>

using namespace std;

#include "guesser.h"
#include "generator.h"

// Call as "pgm filename"
int main(int argc, char **argv)
{
    if(argc != 2) {
        cout << "Usage: " << argv[0] << " encryptedfile" << endl;
        exit(1);
    }

    // 创建猜想对象并打开加密文件。
    guesser trythis;
    if(!trythis.open(argv[1])) {
        cout << "Unable to read " << argv[1] << endl;
        exit(2);
    }

    // 计算解码次数。
    int nkey = 0;

    // 尝试密码,知道成功打开文件。
    generator gen("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
              "0123456789");
    string outfile = argv[1] + string(".plain");
    while(true) {
        string keyguess = gen.next();
        guesser::guess_t res = trythis.guess(keyguess, outfile);
        if(res == guesser::no || res == guesser::no_but_wrote) {
            if((++nkey & 0xffff) == 0) {
                cout << nkey << " keys; current "
                     << keyguess << endl;
            }
            continue;
        } else if(res == guesser::yes) {
            cout << "The key is " << keyguess << ", " 
                 << nkey << " keys guessed." << endl;
            exit(0);
        } else {
            cout << "Unable to process guess " << keyguess << endl;
            exit(1);
        }
    }
}
  • 写回答

1条回答 默认 最新

  • oyljerry 2016-11-01 01:56
    关注

    解码这是CPU密集型的,主要取决于性能,而不是调度,你需要能够优化算法,或者用GPU等来加速运算等

    评论

报告相同问题?

悬赏问题

  • ¥35 平滑拟合曲线该如何生成
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 自己瞎改改,结果现在又运行不了了
  • ¥15 链式存储应该如何解决
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站