You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository has been archived by the owner on Jan 22, 2025. It is now read-only.
use uint::construct_uint;
construct_uint! {
pub struct U128(2);
}
//[...]
fn compare_compute_budget_consumption() {
let x = 17234u64; //some arbitrary number
let y = 3273u64; //some arbitrary number
//u64
let v1 = x*10u64.pow(9);
let v2 = y;
sol_log_compute_units();
let _add = v1+v2;
sol_log_compute_units();
let _sub = v1-v2;
sol_log_compute_units();
let _mul = v1*v2;
sol_log_compute_units();
let _div = v1/v2;
sol_log_compute_units();
//u128
let v1 = (x as u128)*10u128.pow(19);
let v2 = y as u128;
sol_log_compute_units();
let _add = v1+v2;
sol_log_compute_units();
let _sub = v1-v2;
sol_log_compute_units();
let _mul = v1*v2;
sol_log_compute_units();
let _div = v1/v2;
sol_log_compute_units();
//U128
let v1 = U128::from(x)*U128::from(10).pow(19.into());
let v2 = U128::from(y);
sol_log_compute_units();
let _add = v1+v2;
sol_log_compute_units();
let _sub = v1-v2;
sol_log_compute_units();
let _mul = v1*v2;
sol_log_compute_units();
let _div = v1/v2;
sol_log_compute_units();
}
any progress on this?
Tested on solana-test-validator 1.9.30 (src:30e47c2b; feat:462418899)
Same issue, when performing 31545460800000000000000000000000u128 / 120000000000000u128, takes 6854 compute units for this.
Logging a u64 by itself takes 100 compute units. Disable the release build (which almost certainly runs the optimizer which will replace all hardcoded calculations at compile time). Alternatively use Clock::get().unwrap().unix_timestamp as u64 as a dynamic seed for your numbers.
results (adding u64 for comparison):
u64:
add: 5
sub: 5
mul: 5
div: 6
u128:
add: 14
sub: 14
mul: 49
div: 3694 !!!
U128:
add: 8
sub: 8
mul: 8
div: 427
U128 is superior to the built-in u128 in every respect, except for
.leading_zeros()
and has correct overflow behavior (panicks as intended).The text was updated successfully, but these errors were encountered: