i can check the speed test rust and python 3.12
but result is not good i think so rust create to increase python 3.12 code execution speed
please can you explain
Rust Code
use std::time::Instant;
use std::collections::HashMap;
pub fn test_list_operations() {
// Initialize a vector with numbers from 0 to 99999
let mut vec: Vec<usize> = (0..100_000).collect();
let start_time = Instant::now();
// Perform a series of operations 1000 times
for _ in 0..1000 {
// Append the current length of the vector to the end
vec.push(vec.len());
// Remove the first element of the vector
vec.remove(0);
}
// Calculate the total elapsed time
let end_time = start_time.elapsed();
println!("List operations took {:?}", end_time);
}
pub fn test_dict_operations() {
// Initialize a HashMap with keys from 0 to 9999 and values as their squares
let mut dict: HashMap<usize, usize> = (0..10_000).map(|i| (i, i * i)).collect();
// Add the key 0 with a value of 0 to the dictionary
dict.insert(0, 0);
let start_time = Instant::now();
// Perform a series of operations 1000 times
for _ in 0..1000 {
// Insert a new key-value pair where the key is the current size of the dictionary
dict.insert(dict.len(), dict.len() * dict.len());
// Check if the key 0 exists in the dictionary
if dict.contains_key(&0) {
// Remove the key 0 from the dictionary
dict.remove(&0);
}
}
// Calculate the total elapsed time
let end_time = start_time.elapsed();
println!("Dictionary operations took {:?}", end_time);
}
pub fn test_loop() {
let start_time = Instant::now();
// Perform an empty loop 1,000,000 times
for _ in 0..1_000_000 {
// This loop does nothing; it's used to measure the overhead of looping
}
let end_time = start_time.elapsed();
println!("Loop took {:?}", end_time);
}
pub fn test_function_call() {
// Define a dummy function that does nothing
fn dummy_function() {}
let start_time = Instant::now();
// Call the dummy function 1,000,000 times
for _ in 0..1_000_000 {
dummy_function();
}
let end_time = start_time.elapsed();
println!("Function call took {:?}", end_time);
}
fn main() {
test_list_operations();
test_dict_operations();
test_loop();
test_function_call();
}
Output
List operations took 16.311708ms
Dictionary operations took 663.726µs
Loop took 8.946678ms
Function call took 9.223464ms
Python 3.12 code
import time # Import the time module to measure elapsed time for operations
from collections import defaultdict # Import defaultdict for dictionary operations (not used here but can be useful)
def test_list_operations():
# Initialize a list with numbers from 0 to 99999
lst = list(range(100000))
# Record the start time for performance measurement
start_time = time.time()
# Perform a series of operations 1000 times
for _ in range(1000):
# Append the current length of the list to the end of the list
lst.append(len(lst))
# Remove the first element of the list (index 0)
lst.pop(0)
# Calculate the total elapsed time by subtracting start time from the current time
end_time = time.time() - start_time
# Print the time taken for list operations in seconds, formatted to 4 decimal places
print(f"List operations took {end_time:.4f} seconds")
def test_dict_operations():
# Initialize a dictionary with keys from 0 to 9999 and values as their squares
dct = {i: i * i for i in range(10000)}
# Add the key 0 with a value of 0 to the dictionary
dct[0] = 0
# Record the start time for performance measurement
start_time = time.time()
# Perform a series of operations 1000 times
for _ in range(1000):
# Insert a new key-value pair where the key is the current size of the dictionary
dct[len(dct)] = len(dct) * len(dct)
# Check if the key 0 exists in the dictionary
if 0 in dct:
# Remove the key 0 from the dictionary if it exists
del dct[0]
# Calculate the total elapsed time for dictionary operations
end_time = time.time() - start_time
# Print the time taken for dictionary operations in seconds
print(f"Dictionary operations took {end_time:.4f} seconds")
def test_loop():
# Record the start time for measuring the loop duration
start_time = time.time()
# Perform an empty loop 1,000,000 times
for _ in range(1000000):
pass # This loop does nothing; it's used to measure the overhead of looping
# Calculate the total elapsed time for the loop execution
end_time = time.time() - start_time
# Print the time taken for the empty loop in seconds
print(f"Loop took {end_time:.4f} seconds")
def test_function_call():
# Define a dummy function that does nothing
def dummy_function():
pass
# Record the start time for measuring the function call duration
start_time = time.time()
# Call the dummy function 1,000,000 times
for _ in range(1000000):
dummy_function() # Invoke the dummy function
# Calculate the total elapsed time for the function calls
end_time = time.time() - start_time
# Print the time taken for function calls in seconds
print(f"Function call took {end_time:.4f} seconds")
# Main execution block
if __name__ == "__main__":
# Call each test function to measure and display their execution times
test_list_operations()
test_dict_operations()
test_loop()
test_function_call()
Output
List operations took 0.0145 seconds
Dictionary operations took 0.0004 seconds
Loop took 0.0887 seconds
Function call took 0.2298 seconds