LoginSignup
4
1

More than 5 years have passed since last update.

[Review] Tensorflow Multi-Thread Experiment

Last updated at Posted at 2018-06-14

Introduction

This is an experimental article with regard to multi-thread processing in Tensorflow.
Inside the code, there is nothing difficult stuff. It's just basic math addition operation and repeat it 40,000 times. Then compared the running time in two operations.

  1. for loop
  2. Tensorflow Multi-thread

Official docs:https://www.tensorflow.org/versions/r1.2/programmers_guide/threading_and_queues

Code

#!/usr/bin/python
import sys
from time import time
import tensorflow as tf

# Define an arbitrary tensor
x = tf.constant([[1, 2]])

num_threads = int(sys.argv[1])

counter = 0
N_EXAMPLES = 1000 * num_threads
with tf.Session() as sess:
    coord = tf.train.Coordinator()
    threads = tf.train.start_queue_runners(sess=sess, coord=coord)

    print("Starting reading {} examples ... hold tight".format(N_EXAMPLES))
    timer = -time()
    while not coord.should_stop() and counter < N_EXAMPLES:
        sess.run(x)
        counter += 1

    timer += time()
    print("Took {} seconds for {} examples. {} seconds / example".format(timer, N_EXAMPLES, timer / N_EXAMPLES))
    coord.request_stop()
    coord.join(threads)


    counter = 0
    print("Starting reading {} examples ... hold tight".format(N_EXAMPLES))
    timer = -time()

    while counter < N_EXAMPLES:
        sess.run(x)
        counter += 1

    timer += time()
    print("Took {} seconds for {} examples. {} seconds / example".format(timer, N_EXAMPLES, timer / N_EXAMPLES))
    sess.close()

Result

Sorry, since I did't print out the name of the function, it is not explicit which result is which. Above one is for tensorflow multi thread, and the other one is using loop operation.
As you can see, almost the same amount of time is consumed during the operation.

Starting reading 40000 examples ... hold tight
Took 3.0370140075683594 seconds for 40000 examples. 7.592535018920899e-05 seconds / example
Starting reading 40000 examples ... hold tight
Took 3.056126117706299 seconds for 40000 examples. 7.640315294265747e-05 seconds / example

Conclusion

I need to check how efficiently use the tensorflow multi thread operation more!

4
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
1