gemini-code-assist[bot] commented on code in PR #309: URL: https://github.com/apache/tvm-ffi/pull/309#discussion_r2587448497
########## tests/scripts/benchmark_kwargs_wrapper.py: ########## @@ -0,0 +1,85 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +"""Benchmark API overhead of kwargs wrapper.""" + +from __future__ import annotations + +import time +from typing import Any + +from tvm_ffi.utils.kwargs_wrapper import make_kwargs_wrapper + + +def print_speed(name: str, speed: float) -> None: + print(f"{name:<60} {speed} sec/call") Review Comment:  For better readability of the benchmark results, you could format the floating-point number for the speed. For example, using scientific notation makes it easier to compare small numbers. ```suggestion def print_speed(name: str, speed: float) -> None: print(f"{name:<60} {speed:.3e} sec/call") ``` ########## tests/scripts/benchmark_kwargs_wrapper.py: ########## @@ -0,0 +1,85 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +"""Benchmark API overhead of kwargs wrapper.""" + +from __future__ import annotations + +import time +from typing import Any + +from tvm_ffi.utils.kwargs_wrapper import make_kwargs_wrapper + + +def print_speed(name: str, speed: float) -> None: + print(f"{name:<60} {speed} sec/call") + + +def target_func(*args: Any) -> None: # type: ignore[no-untyped-def] + pass + + +def benchmark_kwargs_wrapper(repeat: int = 1000000) -> None: + """Benchmark kwargs wrapper with integer arguments.""" + # Create test arguments + x = 1 + y = 2 + z = 3 + + # Create wrapper with two optional kwargs + wrapper = make_kwargs_wrapper(target_func, ["x", "y", "z"], args_defaults=(None, None)) + + # Benchmark 1: Direct call to target function (baseline) + start = time.time() + for _ in range(repeat): + target_func(x, y, z) + end = time.time() + print_speed("target_func(x, y, z)", (end - start) / repeat) + + # Benchmark 2: Wrapper with all positional arguments + start = time.time() + for _ in range(repeat): + wrapper(x, y, z) + end = time.time() + print_speed("wrapper(x, y, z)", (end - start) / repeat) + + # Benchmark 3: Wrapper with positional + kwargs + start = time.time() + for _ in range(repeat): + wrapper(x, y=y, z=z) + end = time.time() + print_speed("wrapper(x, y=y, z=z)", (end - start) / repeat) + + # Benchmark 4: Wrapper with all kwargs + start = time.time() + for _ in range(repeat): + wrapper(x=x, y=y, z=z) + end = time.time() + print_speed("wrapper(x=x, y=y, z=z)", (end - start) / repeat) + + # Benchmark 5: Wrapper with defaults + start = time.time() + for _ in range(repeat): + wrapper(x) + end = time.time() + print_speed("wrapper(x) [with defaults]", (end - start) / repeat) Review Comment:  For more accurate benchmarking, it's recommended to use `time.perf_counter()` instead of `time.time()`. `perf_counter()` provides a high-resolution monotonic clock that is unaffected by system time changes, making it more suitable for measuring short time intervals. ```python def benchmark_kwargs_wrapper(repeat: int = 1000000) -> None: """Benchmark kwargs wrapper with integer arguments.""" # Create test arguments x = 1 y = 2 z = 3 # Create wrapper with two optional kwargs wrapper = make_kwargs_wrapper(target_func, ["x", "y", "z"], args_defaults=(None, None)) # Benchmark 1: Direct call to target function (baseline) start = time.perf_counter() for _ in range(repeat): target_func(x, y, z) end = time.perf_counter() print_speed("target_func(x, y, z)", (end - start) / repeat) # Benchmark 2: Wrapper with all positional arguments start = time.perf_counter() for _ in range(repeat): wrapper(x, y, z) end = time.perf_counter() print_speed("wrapper(x, y, z)", (end - start) / repeat) # Benchmark 3: Wrapper with positional + kwargs start = time.perf_counter() for _ in range(repeat): wrapper(x, y=y, z=z) end = time.perf_counter() print_speed("wrapper(x, y=y, z=z)", (end - start) / repeat) # Benchmark 4: Wrapper with all kwargs start = time.perf_counter() for _ in range(repeat): wrapper(x=x, y=y, z=z) end = time.perf_counter() print_speed("wrapper(x=x, y=y, z=z)", (end - start) / repeat) # Benchmark 5: Wrapper with defaults start = time.perf_counter() for _ in range(repeat): wrapper(x) end = time.perf_counter() print_speed("wrapper(x) [with defaults]", (end - start) / repeat) ``` -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
