I am using a Python script to handle part of a C++ program, but the process is going extremely slowly.
I have a basic function for running shell commands and retrieving the output:
std::string ShellCommand::RunShellCommand(std::string cmd) {
char buffer[128];
std::string Response = "";
FILE* pipe = popen(cmd.c_str(), "r");
while (!feof(pipe)) {
// use buffer to read and add to result
if (fgets(buffer, 128, pipe) != NULL)
Response += buffer;
}
pclose(pipe);
return Response;
}
I use this command in a loop:
void loop(int data) {
while (HashSize(HashGenerator(data)) > 25) {
data += 1;
}
}
HashSize() takes the string hash and counts the leading zeros by iterating over each character until a non-zero character is found, counting the total binary zeros. The HashGenerator() function uses the generic shell command function above to activate a python script:
import hashlib
import sys
# Get the data from argv
Data = sys.argv[1]
# Generate the hash
Hash = hashlib.sha256()
Hash.update(Data)
# Output the generated hash
print(Hash.hexdigest())
Obviously, the data for the script is taken from the shell command.
This all works fine, but is very slow (data is incremented from 0 to roughly ~18,000 in a few hours). Now, my actual implementation of this is a bit more complicated, but I suspect the system call to Python is what is causing the problems here.
What is the overhead for this process? Do you agree that this is where my problem is? Is there a way I can speed up this procedure?
I want this question to be useful for others, so I want to avoid being overly hardware specific; however, I should mention that this is running on a Raspberry Pi Zero W, which isn't known for breaking computational records. I would still expect it to go faster than this, though.