I want to use electrum to check the balances of bitcoin addresses in my PHP application. Here is how I am currently doing this:
function get_address_balance($btcaddress) {
$output = shell_exec("electrum getaddressbalance ".escapeshellarg($btcaddress));
if ($output=='daemon is not connected'){return False;}
$balances = objectToArray(json_decode($output));
// $balances['confirmed'] and $balances['unconfirmed'];
return $balances;
}
This piece of code can take a very long time to execute, usually a minimum of one second but normally much longer than that. what is the best way to do this?
Electrum is an interpreted Python program. You are restarting it on each run of this code. The Python interpreter needs to load tons of Python scripts before it can even start doing anything useful. Usually starting electrum and getting past initialization takes from 1.5 to 10 seconds, depending on your wallet size and computer speed, amongst other things.
If you want to go faster, you need to be asking a long-running daemon process of some kind, not an electrum client.