from entrypoint2 import entrypoint
import pyscreenshot
import tempfile
import time


def run(force_backend, n, to_file, bbox=None):
    f = tempfile.NamedTemporaryFile(suffix='.png', prefix='test')
    filename = f.name
    start = time.time()
    for i in range(n):
        if to_file:
            pyscreenshot.grab_to_file(filename, backend=force_backend, childprocess=True)
        else:
            pyscreenshot.grab(bbox=bbox, backend=force_backend, childprocess=True)
    end = time.time()
    dt = end - start

    s = ''
    s += '%-20s' % force_backend
    s += '\t'
    s += '%-4.2g sec' % dt
    s += '\t'
    s += '(%5d ms per call)' % (1000.0 * dt / n)
    print(s)


def run_all(n, to_file, bbox=None):
    print('')

    s = ''
    s += 'n=%s' % n
    s += '\t'
    s += ' to_file: %s' % to_file
    s += '\t'
    s += ' bounding box: %s' % (bbox,)
    print(s)

    print('------------------------------------------------------')

    for x in pyscreenshot.backends():
        try:
            run(x, n, to_file, bbox)
        except pyscreenshot.FailedBackendError as e:
            print(e)


@entrypoint
def speedtest():
    n = 10
    run_all(n, True)
    run_all(n, False)
    run_all(n, False, (10, 10, 20, 20))

@@ -76,16 +76,40 @@
 rt time%0A
+import os%0Aimport shutil%0A
 %0A%0Adef ru
@@ -153,17 +153,22 @@
 e):%0A    
-f
+tmpdir
  = tempf
@@ -175,79 +175,50 @@
 ile.
-NamedTemporaryFile(suffix='.png', prefix='
+mkdtemp(prefix='pyscreenshot_speed
 test
+_
 ')%0A
-    filename = f.name%0A
     
@@ -245,17 +245,17 @@
     for 
-i
+_
  in rang
@@ -280,16 +280,75 @@
 o_file:%0A
+            filename=os.path.join(tmpdir, 'speedtest.png')%0A
         
@@ -731,16 +731,47 @@
 rint(s)%0A
+    shutil.rmtree(tmpdir)%0A    %0A
 %0A%0Adef ru

fix temp files