Created
October 4, 2014 23:32
-
-
Save alexandre/bbe830b1a917db7236e2 to your computer and use it in GitHub Desktop.
Avaliando o tempo de execução de funções alternativas a função drop do Haskell
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/python3 | |
| import time | |
| def test(fun, *args): | |
| t1 = time.time() | |
| fun(*args) | |
| t2 = time.time() | |
| return 'Exec. time: {}'.format(t1-t2) | |
| def drop_loop(n, _list): | |
| for x in range(n): | |
| _list.pop(0) | |
| return _list | |
| def drop_rec(n, _list): | |
| if n == 0: | |
| return _list | |
| _list.pop(0) | |
| return drop_rec(n-1, _list) | |
| ''' | |
| Haskell: | |
| Prelude> drop 100 [1..10000] | |
| (0.19 secs, 52914384 bytes) | |
| ''' | |
| # drop com recursão | |
| print('[LOOP] %r ' % test(drop_loop, 100, [x for x in range(10000)])) | |
| # drop com loop | |
| print('[REC] %r ' % test(drop_rec, 100, [x for x in range(10000)])) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment