Is it possible to use std for benchmarks? If not, how can I get timestamps in a benchmark?

I tried using this but it didn't compile:

 #[cfg(feature = "runtime-benchmarks")] extern crate std;
  • Can you give a bit more context to your question? What exactly are you doing, what error are you getting? Why would you need timestamps in the first place? Oct 26, 2022 at 9:30
  • I would like to access timestamps to calculate durations of sections of code. Oct 27, 2022 at 12:45

2 Answers 2


You should use your benchmarking setup to insert a fake timestamp into your runtime, and use that for the execution of your code. Similar to how you would insert fake balances or other storage items into your benchmarking setup in order to properly execute the benchmark.

As you can see, the set_timestamp function is made available to benchmarks: https://github.com/paritytech/substrate/blob/master/frame/timestamp/src/lib.rs#L283

impl<T: Config> Pallet<T> {
    /// Get the current time for the current block.
    /// NOTE: if this function is called prior to setting the timestamp,
    /// it will return the timestamp of the previous block.
    pub fn get() -> T::Moment {

    /// Set the timestamp to something in particular. Only used for tests.
    #[cfg(any(feature = "runtime-benchmarks", feature = "std"))]
    pub fn set_timestamp(now: T::Moment) {
        <T::OnTimestampSet as OnTimestampSet<_>>::on_timestamp_set(now);

You can get the on-chain time from the timestamp pallet with Timestamp::now().
But this does not make sense for benchmarking. Benchmarks and their code run in WASM no-std. There are two ways this goes:

  • Your code is std: The code will therefore never be part of the runtime. Use criterion or any other benchmarking library you like and optimize it.
  • Your code is no-std: This part is covered by FRAME benchmarking. It records timestamps before and after executing the WASM code and calculates the weight of it.

So I don't see why you want mix std timestamps + FRAME benchmarking.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.