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
I compared the elapsed time of a small program (in both C and Rust) that calls the double-precisionsin() function 1 million times. Rust version was more than 2 times slower compared to the C version.
intmain(void)
{
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */HAL_Init();
/* Configure the system clock */SystemClock_Config();
MX_GPIO_Init();
MX_USART3_UART_Init();
doublex=0.0;
inti=0;
ITM_SendChar('A'); // Start measuring time --------for(i=0; i<1000000; i++)
x+=sin( (double) i );
ITM_SendChar('A'); // Finish measuring time --------printf("\n%f\n", x);
while(1) {}
}
Rust program (uses rust-lang/libm)
#[entry]fnmain() -> ! {iflet(Some(dp),Some(cp)) = (
stm32::Peripherals::take(),
cortex_m::peripheral::Peripherals::take(),){// Set up the system clock. We want to run at 168MHz for this one.let rcc = dp.RCC.constrain();let clocks = rcc.cfgr.sysclk(168.mhz()).freeze();
cortex_m::asm::bkpt();// BKPT: Start measuring time --------letmut x:f64 = 0.0;for i in0..1_000_000{// Call `libm::sin()` 1 million times!
x += libm::sin(i asf64);}
cortex_m::asm::bkpt();// BKPT: Finish measuring time --------hprintln!("{:.6}", x).unwrap();}loop{continue;}}
The change made the program take 2.74 seconds less than the program using the sin implementation of musl libc.
However, the performance gap is still significant between C and Rust 😿 Since the performance gap is significant even when the source codes are equivalent,
it seems that this issue has to be handled at the compiler level.
The text was updated successfully, but these errors were encountered:
Issue
I compared the elapsed time of a small program (in both C and Rust) that calls the double-precision
sin()
function 1 million times. Rust version was more than 2 times slower compared to the C version.NucleoF429ZI
boardnewlib
,gcc
)thumbv7em-none-eabihf
)C & Rust program code comparison
rust-lang/libm
)What I tried so far
rust-lang/libm::sin
was ported frommusl libc
, and its implementation is slightly different fromnewlib
's sin() function. In my fork ofrust-lang/libm
, I changed the implementation ofrust-lang/libm::sin
to be equivalent tonewlib libm
's sin() function. Below is the performance comparison.NucleoF429ZI
boardnewlib
,gcc
)sin
impl equivalent tomusl libc
)sin
impl equivalent tonewlib
)The change made the program take 2.74 seconds less than the program using the
sin
implementation ofmusl libc
.However, the performance gap is still significant between C and Rust 😿
Since the performance gap is significant even when the source codes are equivalent,
it seems that this issue has to be handled at the compiler level.
The text was updated successfully, but these errors were encountered: