Personal website Rudolf Adamkovič

Home / TensorFlow / Performance benchmark


Benchmark

import tensorflow as tf
import numpy as np
import timeit

def np_add(a, b):
    y = np.copy(a)
    for i in range(b):
        y += 1
    return y

def tf_add_eager(a, b):
    y = tf.identity(a)
    for i in range(b):
        y += tf.constant(1, dtype=a.dtype)
    return y

tf_add_graph = tf.function(tf_add_eager)

a_shape = (20, 20)
b = 1000 - 1

tf_a, tf_b = (tf.ones(a_shape), b)
np_a, np_b = (np.ones(a_shape), b)

results = [["Runs", "NumPy", "TensorFlow Eager", "TensorFlow Graph"], None]

for runs in [1, 2, 10, 100, 1000]:
    row = [runs]
    for function in [lambda : np_add(np_a, np_b),
                     lambda : tf_add_eager(tf_a, tf_b),
                     lambda : tf_add_graph(tf_a, tf_b)]:
        seconds = timeit.timeit(function, number = runs)
        milliseconds_per_run = (seconds / runs) * 1000
        row.append(f"{milliseconds_per_run:.4f}")
    results.append(row)

results
Runs NumPy TensorFlow Eager TensorFlow Graph
1 1.2050 47.8199 266.5787
2 0.7031 33.2319 0.2714
10 0.6882 32.9146 0.1607
100 0.6879 33.1700 0.1417
1000 0.7048 33.1270 0.1360

© 2025 Rudolf Adamkovič under GNU General Public License version 3.
Made with Emacs and secret alien technologies of yesteryear.