File tree 7 files changed +141
-0
lines changed 7 files changed +141
-0
lines changed Original file line number Diff line number Diff line change
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
+ ///|
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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"
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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 {}
You can’t perform that action at this time.
0 commit comments