|
5 | 5 |
|
6 | 6 | import time |
7 | 7 | from numpy.testing import assert_allclose, assert_ |
8 | | -from quantecon.util import tic, tac, toc, loop_timer, Timer |
| 8 | +from quantecon.util import tic, tac, toc, loop_timer, Timer, timeit |
9 | 9 |
|
10 | 10 |
|
11 | 11 | class TestTicTacToc: |
@@ -147,3 +147,116 @@ def test_timer_exception_handling(self): |
147 | 147 | # Timer should still record elapsed time |
148 | 148 | assert timer.elapsed is not None |
149 | 149 | assert_allclose(timer.elapsed, self.sleep_time, atol=0.05, rtol=2) |
| 150 | + |
| 151 | + def test_timeit_basic(self): |
| 152 | + """Test basic timeit functionality.""" |
| 153 | + def test_func(): |
| 154 | + time.sleep(self.sleep_time) |
| 155 | + |
| 156 | + result = timeit(test_func, runs=3, silent=True) |
| 157 | + |
| 158 | + # Check that we have results |
| 159 | + assert 'elapsed' in result |
| 160 | + assert 'average' in result |
| 161 | + assert 'minimum' in result |
| 162 | + assert 'maximum' in result |
| 163 | + assert isinstance(result['elapsed'], list) |
| 164 | + assert len(result['elapsed']) == 3 |
| 165 | + |
| 166 | + # Check timing accuracy |
| 167 | + for run_time in result['elapsed']: |
| 168 | + assert_allclose(run_time, self.sleep_time, atol=0.05, rtol=2) |
| 169 | + |
| 170 | + assert_allclose(result['average'], self.sleep_time, atol=0.05, rtol=2) |
| 171 | + assert result['minimum'] <= result['average'] <= result['maximum'] |
| 172 | + |
| 173 | + def test_timeit_lambda_function(self): |
| 174 | + """Test timeit with lambda functions for arguments.""" |
| 175 | + def test_func_with_args(sleep_time, multiplier=1): |
| 176 | + time.sleep(sleep_time * multiplier) |
| 177 | + |
| 178 | + # Use lambda to bind arguments |
| 179 | + func_with_args = lambda: test_func_with_args(self.sleep_time, 0.5) |
| 180 | + result = timeit(func_with_args, runs=2, silent=True) |
| 181 | + |
| 182 | + # Check results |
| 183 | + assert len(result['elapsed']) == 2 |
| 184 | + for run_time in result['elapsed']: |
| 185 | + assert_allclose(run_time, self.sleep_time * 0.5, atol=0.05, rtol=2) |
| 186 | + |
| 187 | + def test_timeit_validation(self): |
| 188 | + """Test validation for timeit function.""" |
| 189 | + def test_func(): |
| 190 | + time.sleep(self.sleep_time) |
| 191 | + |
| 192 | + # Test invalid runs parameter |
| 193 | + try: |
| 194 | + timeit(test_func, runs=0) |
| 195 | + assert False, "Should have raised ValueError" |
| 196 | + except ValueError as e: |
| 197 | + assert "runs must be a positive integer" in str(e) |
| 198 | + |
| 199 | + try: |
| 200 | + timeit(test_func, runs=-1) |
| 201 | + assert False, "Should have raised ValueError" |
| 202 | + except ValueError as e: |
| 203 | + assert "runs must be a positive integer" in str(e) |
| 204 | + |
| 205 | + # Test invalid function |
| 206 | + try: |
| 207 | + timeit("not a function", runs=1) |
| 208 | + assert False, "Should have raised ValueError" |
| 209 | + except ValueError as e: |
| 210 | + assert "func must be callable" in str(e) |
| 211 | + |
| 212 | + def test_timeit_single_run(self): |
| 213 | + """Test that timeit works with single run.""" |
| 214 | + def test_func(): |
| 215 | + time.sleep(self.sleep_time) |
| 216 | + |
| 217 | + result = timeit(test_func, runs=1, silent=True) |
| 218 | + |
| 219 | + assert len(result['elapsed']) == 1 |
| 220 | + assert result['average'] == result['elapsed'][0] |
| 221 | + assert result['minimum'] == result['elapsed'][0] |
| 222 | + assert result['maximum'] == result['elapsed'][0] |
| 223 | + |
| 224 | + def test_timeit_different_units(self): |
| 225 | + """Test timeit with different time units.""" |
| 226 | + def test_func(): |
| 227 | + time.sleep(self.sleep_time) |
| 228 | + |
| 229 | + # Test milliseconds (silent mode to avoid output during tests) |
| 230 | + result_ms = timeit(test_func, runs=2, unit="milliseconds", silent=True) |
| 231 | + assert len(result_ms['elapsed']) == 2 |
| 232 | + |
| 233 | + # Test microseconds |
| 234 | + result_us = timeit(test_func, runs=2, unit="microseconds", silent=True) |
| 235 | + assert len(result_us['elapsed']) == 2 |
| 236 | + |
| 237 | + # All results should be in seconds regardless of display unit |
| 238 | + for run_time in result_ms['elapsed']: |
| 239 | + assert_allclose(run_time, self.sleep_time, atol=0.05, rtol=2) |
| 240 | + for run_time in result_us['elapsed']: |
| 241 | + assert_allclose(run_time, self.sleep_time, atol=0.05, rtol=2) |
| 242 | + |
| 243 | + def test_timeit_stats_only(self): |
| 244 | + """Test timeit with stats_only option.""" |
| 245 | + def test_func(): |
| 246 | + time.sleep(self.sleep_time) |
| 247 | + |
| 248 | + # This test is mainly to ensure stats_only doesn't crash |
| 249 | + result = timeit(test_func, runs=2, stats_only=True, silent=True) |
| 250 | + assert len(result['elapsed']) == 2 |
| 251 | + |
| 252 | + def test_timeit_invalid_timer_kwargs(self): |
| 253 | + """Test that invalid timer kwargs are rejected.""" |
| 254 | + def test_func(): |
| 255 | + time.sleep(self.sleep_time) |
| 256 | + |
| 257 | + try: |
| 258 | + timeit(test_func, runs=1, invalid_param="test") |
| 259 | + assert False, "Should have raised ValueError" |
| 260 | + except ValueError as e: |
| 261 | + assert "Unknown timer parameters" in str(e) |
| 262 | + |
0 commit comments