Skip to content

Commit edeed8d

Browse files
committed
Factor out time_spent computation
1 parent e839c67 commit edeed8d

File tree

2 files changed

+28
-11
lines changed

2 files changed

+28
-11
lines changed

mypy/build.py

+12-11
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@
3636
from mypy.errors import Errors, CompileError, ErrorInfo, report_internal_error
3737
from mypy.util import (
3838
DecodeError, decode_python_encoding, is_sub_path, get_mypy_comments, module_prefix,
39-
read_py_file, hash_digest, is_typeshed_file, is_stub_package_file, get_top_two_prefixes
39+
read_py_file, hash_digest, is_typeshed_file, is_stub_package_file, get_top_two_prefixes,
40+
time_ref, time_spent_us
4041
)
4142
if TYPE_CHECKING:
4243
from mypy.report import Reports # Avoid unconditional slow import
@@ -2039,7 +2040,7 @@ def parse_file(self) -> None:
20392040
else:
20402041
manager.log("Using cached AST for %s (%s)" % (self.xpath, self.id))
20412042

2042-
t0 = time.perf_counter()
2043+
t0 = time_ref()
20432044

20442045
with self.wrap_context():
20452046
source = self.source
@@ -2086,7 +2087,7 @@ def parse_file(self) -> None:
20862087
self.tree.ignored_lines,
20872088
self.ignore_all or self.options.ignore_errors)
20882089

2089-
self.time_spent_us += int((time.perf_counter() - t0) * 1e6)
2090+
self.time_spent_us += time_spent_us(t0)
20902091

20912092
if not cached:
20922093
# Make a copy of any errors produced during parse time so that
@@ -2123,7 +2124,7 @@ def semantic_analysis_pass1(self) -> None:
21232124
options = self.options
21242125
assert self.tree is not None
21252126

2126-
t0 = time.perf_counter()
2127+
t0 = time_ref()
21272128

21282129
# Do the first pass of semantic analysis: analyze the reachability
21292130
# of blocks and import statements. We must do this before
@@ -2143,7 +2144,7 @@ def semantic_analysis_pass1(self) -> None:
21432144
if options.allow_redefinition:
21442145
# Perform more renaming across the AST to allow variable redefinitions
21452146
self.tree.accept(VariableRenameVisitor())
2146-
self.time_spent_us += int((time.perf_counter() - t0) * 1e6)
2147+
self.time_spent_us += time_spent_us(t0)
21472148

21482149
def add_dependency(self, dep: str) -> None:
21492150
if dep not in self.dependencies_set:
@@ -2201,10 +2202,10 @@ def compute_dependencies(self) -> None:
22012202
def type_check_first_pass(self) -> None:
22022203
if self.options.semantic_analysis_only:
22032204
return
2204-
t0 = time.perf_counter()
2205+
t0 = time_ref()
22052206
with self.wrap_context():
22062207
self.type_checker().check_first_pass()
2207-
self.time_spent_us += int((time.perf_counter() - t0) * 1e6)
2208+
self.time_spent_us += time_spent_us(t0)
22082209

22092210
def type_checker(self) -> TypeChecker:
22102211
if not self._type_checker:
@@ -2222,17 +2223,17 @@ def type_map(self) -> Dict[Expression, Type]:
22222223
def type_check_second_pass(self) -> bool:
22232224
if self.options.semantic_analysis_only:
22242225
return False
2225-
t0 = time.perf_counter()
2226+
t0 = time_ref()
22262227
with self.wrap_context():
22272228
return self.type_checker().check_second_pass()
2228-
self.time_spent_us += int((time.perf_counter() - t0) * 1e6)
2229+
self.time_spent_us += time_spent_us(t0)
22292230

22302231
def finish_passes(self) -> None:
22312232
assert self.tree is not None, "Internal error: method must be called on parsed file only"
22322233
manager = self.manager
22332234
if self.options.semantic_analysis_only:
22342235
return
2235-
t0 = time.perf_counter()
2236+
t0 = time_ref()
22362237
with self.wrap_context():
22372238
# Some tests (and tools) want to look at the set of all types.
22382239
options = manager.options
@@ -2255,7 +2256,7 @@ def finish_passes(self) -> None:
22552256
self.free_state()
22562257
if not manager.options.fine_grained_incremental and not manager.options.preserve_asts:
22572258
free_tree(self.tree)
2258-
self.time_spent_us += int((time.perf_counter() - t0) * 1e6)
2259+
self.time_spent_us += time_spent_us(t0)
22592260

22602261
def free_state(self) -> None:
22612262
if self._type_checker:

mypy/util.py

+16
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import hashlib
99
import io
1010
import shutil
11+
import time
1112

1213
from typing import (
1314
TypeVar, List, Tuple, Optional, Dict, Sequence, Iterable, Container, IO, Callable
@@ -763,3 +764,18 @@ def is_stub_package_file(file: str) -> bool:
763764

764765
def unnamed_function(name: Optional[str]) -> bool:
765766
return name is not None and name == "_"
767+
768+
769+
if sys.version_info.major >= 3 and sys.version_info.minor >= 7:
770+
time_ref = time.perf_counter_ns
771+
772+
773+
def time_spent_us(t0: int) -> int:
774+
return (time.perf_counter_ns() - t0) // 1000
775+
else:
776+
time_ref = time.perf_counter
777+
778+
779+
def time_spent_us(t0: float) -> int:
780+
return int((time.perf_counter() - t0) * 1e6)
781+

0 commit comments

Comments
 (0)