Skip to content

Commit 0309f04

Browse files
waterlensbobzhang
authored andcommitted
feat: monotonic clock
1 parent 791a175 commit 0309f04

7 files changed

+141
-0
lines changed

bench/bench.mbt

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Copyright 2025 International Digital Economy Academy
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
///|

bench/bench_test.mbt

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Copyright 2025 International Digital Economy Academy
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
///|
16+
test "monotonic clock" {
17+
let ts = monotonic_clock_start()
18+
let mut sum: UInt64 = 0
19+
for i in 0..=100000 {
20+
sum += i.to_uint64()
21+
}
22+
let diff = monotonic_clock_end(ts)
23+
println("elapsed time: \{diff} us")
24+
assert_true!(diff > 0.0)
25+
assert_false!(diff.is_inf())
26+
assert_eq!(sum, 5000050000UL)
27+
}

bench/monotonic_clock_js.mbt

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Copyright 2025 International Digital Economy Academy
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
///|
15+
extern type Timestamp
16+
///|
17+
pub extern "js" fn monotonic_clock_start() -> Timestamp =
18+
#| () => performance.now()
19+
20+
///|
21+
pub extern "js" fn monotonic_clock_end(ts : Timestamp) -> Double =
22+
#| (ts) => (performance.now() - ts) * 1000.0

bench/monotonic_clock_native.mbt

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Copyright 2025 International Digital Economy Academy
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
///|
15+
extern type Timestamp
16+
17+
///|
18+
pub extern "C" fn monotonic_clock_start() -> Timestamp = "moonbit_monotonic_clock_start"
19+
20+
///|
21+
pub extern "C" fn monotonic_clock_end(ts : Timestamp) -> Double = "moonbit_monotonic_clock_stop"

bench/monotonic_clock_wasm.mbt

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Copyright 2025 International Digital Economy Academy
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
///|
15+
16+
extern type Instant
17+
18+
fn instant_elapsed_as_secs_f64(x : Instant) -> Double = "__moonbit_time_unstable" "instant_elapsed_as_secs_f64"
19+
20+
///|
21+
pub fn monotonic_clock_start() -> Instant = "__moonbit_time_unstable" "instant_now"
22+
23+
///|
24+
pub fn monotonic_clock_end(ts : Instant) -> Double {
25+
let elapsed_secs: Double = instant_elapsed_as_secs_f64(ts);
26+
elapsed_secs * 1000000.0
27+
}

bench/moon.pkg.json

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"targets": {
3+
"monotonic_clock_js.mbt": ["js"],
4+
"monotonic_clock_wasm.mbt": ["wasm", "wasm-gc"],
5+
"monotonic_clock_native.mbt": ["native"]
6+
},
7+
"import": [
8+
"moonbitlang/core/builtin"
9+
],
10+
"test-import": [
11+
"moonbitlang/core/double"
12+
]
13+
}

bench/types.mbt

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Copyright 2025 International Digital Economy Academy
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
///|
16+
pub(all) struct T {}

0 commit comments

Comments
 (0)