Is there a way to speed up this script?

First of all, I know absolutely nothing about c++. I develop with Python and I complained about speed, someone suggested I should use c++, so I went to chatgpt and got a c++ script but it is slower than my python script. My python script does 1400 iterations/s while the c++ script does 60 iterations/s.
It is a read and write script that loads a text file containing mnemonics, calculates the valid ones and write them out to another text file.
If i can get 5x the speed of my python script, I am ok. Any help would be appreciated.

#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <bitset>
#include <vector>
#include <openssl/sha.h>
#include <algorithm>
#include <stdexcept>
#include <unordered_map>


std::string normalize_string(const std::string& mnemonic) {
    std::string normalized = mnemonic;
    std::transform(normalized.begin(), normalized.end(), normalized.begin(), ::tolower);
    return normalized;
}

std::string calculate_checksum(const std::string& binary_string) {
    unsigned char hash[SHA256_DIGEST_LENGTH];
    SHA256_CTX sha256;
    SHA256_Init(&sha256);
    SHA256_Update(&sha256, binary_string.c_str(), binary_string.size());
    SHA256_Final(hash, &sha256);
    
    std::string checksum;
    for (int i = 0; i <SHA256_DIGEST_LENGTH; ++i) {
        checksum += std::bitset<8>(hash[i]).to_string();
    }
    
    return checksum.substr(0, binary_string.size() / 32);;
}

int get_word_index(const std::string& word) {
    std::unordered_map<std::string, int> wordlist = {
        {"abandon", 0}, {"ability", 1}.... //all 2048 bip39 english words 
    };
    auto it = wordlist.find(word);
    if (it != wordlist.end()) {
        return it->second;
    } else {
        return -1;
    }
}

bool is_mnemonic_valid(const std::string& mnemonic) {
    std::string normalized_mnemonic = normalize_string(mnemonic);
    std::istringstream iss(normalized_mnemonic);
    std::vector<std::string> words;
    std::string word;
    
    while (iss >> word) {
        words.push_back(word);
    }
    
    int num_words = words.size();
    
    if (num_words % 3 != 0 || num_words < 12 || num_words > 24) {
        return false;
    }
    
    std::string binary_string;
    for (const auto& word : words) {
        int index = get_word_index(word);
        if (index == -1) {
            return false;
        }
        
        std::string binary_index = std::bitset<11>(index).to_string();
        binary_string += binary_index;
        
    }
    
    std::string checksum = calculate_checksum(binary_string);
    
    int entropy_size = num_words * 11 - num_words / 3;
    std::string stored_checksum = binary_string.substr(entropy_size);
    
    return checksum == stored_checksum;
}


void process_mnemonics(const std::string& input_file_name, const std::string& output_file_name) {
    std::ifstream input_file(input_file_name);
    std::ofstream output_file(output_file_name);
    
    
    if (!input_file.is_open()) {
        std::cerr << "Error opening input file." << input_file_name << std::endl;
        return;
    }
    
    if (!output_file.is_open()) {
        std::cerr << "Error opening output file." << output_file_name << std::endl;
        return;
    }
    
    std::string mnemonic;
    int total_mnemonics = 0;
    int valid_mnemonics = 0;
    
    while (std::getline(input_file, mnemonic)) {
        total_mnemonics++;
        std::string normalized_mnemonic = normalize_string(mnemonic);
        bool is_valid = is_mnemonic_valid(normalized_mnemonic);
        if (is_valid) {
            output_file << normalized_mnemonic << std::endl;
            valid_mnemonics++;
        }
        
        std::cout << " Processed " << total_mnemonics << " mnemonics. Found " << valid_mnemonics << " valid. " << std::endl;
        
    }
    
    std::cout << "Processing complete. Total: " << total_mnemonics << ", Valid: " << valid_mnemonics << std::endl;
    
    input_file.close();
    output_file.close();
}

int main() {
    std::string input_file_name = "gain1.txt";
    std::string output_file_name = "valid.txt";
    process_mnemonics(input_file_name, output_file_name);
    return 0;
}

New contributor

Sirkastiq is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật