1# Copyright 2017 The TensorFlow Authors. All Rights Reserved. 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"""Benchmarks for `tf.data.Dataset.filter()`.""" 16from tensorflow.python.data.benchmarks import benchmark_base 17from tensorflow.python.data.ops import dataset_ops 18from tensorflow.python.ops import array_ops 19 20 21class FilterBenchmark(benchmark_base.DatasetBenchmarkBase): 22 """Benchmarks for `tf.data.Dataset.filter()`.""" 23 24 def _benchmark(self, predicate, name, benchmark_id): 25 num_elements = 100000 26 dataset = dataset_ops.Dataset.from_tensors(True) 27 dataset = dataset.repeat().filter(predicate) 28 self.run_and_report_benchmark( 29 dataset, 30 num_elements=num_elements, 31 extras={ 32 "model_name": "filter.benchmark.%d" % benchmark_id, 33 "parameters": "%d" % num_elements, 34 }, 35 name=name) 36 37 def benchmark_simple_function(self): 38 self._benchmark(array_ops.identity, "simple_function", benchmark_id=1) 39 40 def benchmark_return_component_optimization(self): 41 self._benchmark(lambda x: x, "return_component", benchmark_id=2) 42 43 44if __name__ == "__main__": 45 benchmark_base.test.main() 46