File: print_example.py

package info (click to toggle)
python-scipy 0.18.1-2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 75,464 kB
  • ctags: 79,406
  • sloc: python: 143,495; cpp: 89,357; fortran: 81,650; ansic: 79,778; makefile: 364; sh: 265
file content (44 lines) | stat: -rw-r--r-- 1,056 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
from __future__ import absolute_import, print_function

import sys
sys.path.insert(0,'..')
import inline_tools

import time


def print_compare(n):
    print('Printing %d integers:' % n)
    t1 = time.time()
    for i in range(n):
        print(i, end=' ')
    t2 = time.time()
    py = (t2-t1)

    # get it in cache
    inline_tools.inline('printf("%d",i);',['i'])
    t1 = time.time()
    for i in range(n):
        inline_tools.inline('printf("%d",i);',['i'])
    t2 = time.time()
    print(' speed in python:', py)
    print(' speed in c:',(t2 - t1))
    print(' speed up: %3.2f' % (py/(t2-t1)))


def cout_example(lst):
    # get it in cache
    i = lst[0]
    inline_tools.inline('std::cout << i << std::endl;',['i'])
    t1 = time.time()
    for i in lst:
        inline_tools.inline('std::cout << i << std::endl;',['i'])
    t2 = time.time()

if __name__ == "__main__":
    n = 3000
    print_compare(n)
    print("calling cout with integers:")
    cout_example([1,2,3])
    print("calling cout with strings:")
    cout_example(['a','bb', 'ccc'])