Understand The Consumption of Calculation Unit on Solana
AS Developers, Construction Applications on the Solana Blockchain, You Are Probable Curious to Know How Your Code is Executed. One aspect of this process is to calculate the consumption of the unit (CU), which can vary depding on the instructions used in your program. In this article, we will explore the instructions consume differentities of calculation units on solana and provide examples to illustrate thesis differentences.
What are the calculation units?
On Solana, A Calculation Unit is a Measure of the Calculation Effort Required to Perform An Instruction. It is Calculated by Dividing the Number of Gases (The Native Cryptocurrency Used for Transaction Costs) Required to Execute the Instruction By Block Time (The Time Required to Execute A Transaction on The Network).
Why do instructions Consume Different Quantities or Calculation Units?
The Main Reason for This Variation is The Difference in the Number of Gas and Complexity for Calculating Each instruction. The Instructions Are Classified Into Several Types, in particular:
- Gas Economical Instructions : These instructions have a loower gas cost but require more calculation. The examples include “call”, “load” and “store”.
- High Gas instructions : These instructions have a higher gas cost but require less calculation. The examples include “Transfer” and “Send”.
The Number of Gases Required to Execute An Instruction is determined by its complexity, which includes factors Such as address size, data type and calculation requirements.
Measure the Consumption of Cu on Solana
To understand how these instructions consume calculation units on solana, we will create a typed test test that performs one of our ix (personalized instruction) and Measures the use of cu. We will compare the results on Various high gas and gas efficient instructions.
`TypeScript
Import * as solana from '@ solana / web3.js';
Import {ix} the ./IX '; // Import Your Personalized IX Implementation
// Create a Solana Instance With Our Test Network (for Example, The Solana Test Network)
Constolaneininstance = New Solana.web3 ();
Const network = 'test network';
// Perform Personalized IX on the Test Network
IX.RUN (Solanain Instance, {
Address: "your_address",
Data: "Your_data",
});
”
Measure The Consumption of Cu
We will use a library called “solana-cpu” to measure unit calculation consumption. You can install it using NPM:
`Bash
NPM Install Solana-CPU
”
Create An Index File (CPU.JS
) With the Following Code:
`JavaScript
Import * as CPU from "Solana-Cpu";
Const IX = Require ('./ IX');
// Create a Solana Instance With Our Test Network
Constolaneininstance = New Solana.web3 ();
Measureecpuusing function () {
// Execute Personalized IX on the Test Network and Measure CU Consumption
Const ctx = {};
Return Cpu.measureecpuusage (
IX.RUN,
'Your_address',
Data: 'Your_data'
));
}
// Measure Unit Calculation Consumption for Different Gas Efficient Instructions
Constal Gaseffitiststructions = ["Call", "Charge", "Store"];
Console.log (cu use on $ {gasficits instructions.join (‘,’)}}};
console.log (Measurement ());
// Measures Unit Calculation Consumption for High Gas Instructions
Const high gas instructions = [“transfer”, “send”];
Console.log (cu use on $ {high gas instructions.join (',')}}};
console.log (Measurement ());
”
Exit Example
The Execution of the Above Code Will Release A Json Object with the Measured Consumption. The exact results may vary depending on your solana body and your test network.
“ Json
{
“Gasffitist Constructions”: {
“Call”: {
“CPU_usage”: “0.01”
},
“Load”: {
“CPU_usage”: “0.02”
},
“Store”: {
“CPU_usage”: “0.03”
}
},
“High gas instructions”: {
“Transfer”: {
“CPU_usage”: “0.05”
},
“Send”: {
“CPU_usage”: “0.