Replies: 3 comments 3 replies
-
This might sound unlikely, but a somewhat common issue is not using release build when large unexpected performance changes are measured. The one other thing that comes to mind is if any multi-threading and hence contention of the GIL might be involved? |
Beta Was this translation helpful? Give feedback.
-
Have you attempted any profiling? There is a brief example of how to do this in |
Beta Was this translation helpful? Give feedback.
-
The same thing happened to me, I have created this simple function: use pyo3::prelude::*;
use std::time::{Instant};
#[pyfunction]
fn rust_sleep() -> i32 {
let start = Instant::now();
let num = 1 + 1;
let duration = start.elapsed();
println!("{:?}", duration);
num
}
#[pymodule]
fn pythonicsqlrust(_py: Python, m: &PyModule) -> PyResult<()> {
m.add_function(wrap_pyfunction!(rust_sleep, m)?)?;
Ok(())
} import time
import pythonicsqlrust
def main():
s = time.time_ns()
print(pythonicsqlrust.rust_sleep())
e = time.time_ns()
print(e - s)
main() In pure rust it takes ~60ns and when called in python ~22350ns. Can you help understand why this happens? (is slower than running in pure python) |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Hi community,
I am having a project, where I provide a service in rust, both via an HTTP server and via PyO3 bindings.
Let's call this function/service
test_service
. Within the function, I measure the duration of specific operations during the execution and return it in the endpoint's response. Meaning, I do not have anything dependent on the server or the bindings, which is taken into consideration, when doing the measurement.However, when I do try to get a response from the service via the HTTP server, I get durations of these operations of something like 20 microseconds, whereas, if I do call exactly the same function via the pyo3 bindings, I get something like 400 microseconds per operation (normalized), which is quite a significant difference in performance. The operations are not by any way dependent on external services.
I wanted to ask, if you are aware of some reason, which could be the cause of this discrepancy in the performance. Thanks ! :)
Beta Was this translation helpful? Give feedback.
All reactions