Уведомления

Группа в Telegram: @pythonsu

#1 Дек. 7, 2008 03:45:34

ZZZ
От: Москва
Зарегистрирован: 2008-04-03
Сообщения: 2161
Репутация: +  26  -
Профиль   Адрес электронной почты  

Странности теста...

Пространства для Вашего Я, змееусты!

Shiza попросил меня сравнить питоньи (CPython) нити с PyQt'шными. Я написал тест.

# -*- coding: utf-8 -*-

import timeit

def test_func():
"""Тестовая функция."""
x = 1000000
while x > 0:
x -= 1


import threading

def python_thread_test(threads_num=3, test_func=test_func):
"""Тестируем питоньи нити."""

class Thread(threading.Thread):
def run(self):
test_func()

threads = []
for _ in xrange(threads_num):
threads.append(Thread())
for thread in threads:
thread.start()
for thread in threads:
thread.join()

timer = timeit.Timer('python_thread_test(3)',
'from __main__ import python_thread_test')
print "Python: %.2f" % timer.timeit(10)
del timer
del threading
del python_thread_test


from PyQt4 import QtCore
app = QtCore.QCoreApplication([])

def PyQt4_thread_test(threads_num=3, test_func=test_func):
"""Тестируем КьюТэшные нити."""

class Thread(QtCore.QThread):
def run(self):
test_func()

threads = []
for _ in xrange(threads_num):
threads.append(Thread())
for thread in threads:
thread.start()
for thread in threads:
thread.wait()

timer = timeit.Timer('PyQt4_thread_test(3)',
'from __main__ import PyQt4_thread_test')
print "PyQt4: %.2f" % timer.timeit(10)
STDOUT
Python: 5.98
PyQt4: 5.42
Пока всё в порядке, но в процессе “игры” с кодом я изменил первый тест:
# -*- coding: utf-8 -*-

import timeit

def test_func():
"""Тестовая функция."""
x = 1000000
while x > 0:
x -= 1


from threading import Thread

def python_thread_test(threads_num=3, test_func=test_func):
"""Тестируем питоньи нити."""

threads = []
for _ in xrange(threads_num):
threads.append(Thread(target=test_func))
for thread in threads:
thread.start()
for thread in threads:
thread.join()

timer = timeit.Timer('python_thread_test(3)',
'from __main__ import python_thread_test')
print "Python: %.2f" % timer.timeit(10)
del timer
del Thread
del python_thread_test


from PyQt4 import QtCore
app = QtCore.QCoreApplication([])

def PyQt4_thread_test(threads_num=3, test_func=test_func):
"""Тестируем КьюТэшные нити."""

class Thread(QtCore.QThread):
def run(self):
test_func()

threads = []
for _ in xrange(threads_num):
threads.append(Thread())
for thread in threads:
thread.start()
for thread in threads:
thread.wait()

timer = timeit.Timer('PyQt4_thread_test(3)',
'from __main__ import PyQt4_thread_test')
print "PyQt4: %.2f" % timer.timeit(10)
STDOUT
Python: 7.17
PyQt4: 7.27
Ещё раз обращаю ваше внимание на то, что был изменён только первый тест.
Но какого чёрта он так повлиял на второй?

P.S. Всё это просто академический интерес, но если кто-нить знает, куда копнуть…



Офлайн

#2 Дек. 7, 2008 13:03:44

Александр Кошелев
От: Москва
Зарегистрирован: 2007-02-03
Сообщения: 1724
Репутация: +  2  -
Профиль   Отправить e-mail  

Странности теста...

Это всё и правда в одном модуле?



Офлайн

#3 Дек. 7, 2008 23:20:51

ZZZ
От: Москва
Зарегистрирован: 2008-04-03
Сообщения: 2161
Репутация: +  26  -
Профиль   Адрес электронной почты  

Странности теста...

Да. И я не вижу никакого смысла разбивать на модули такой тест. Конечно, реальный код я бы оформил по-другому.
Ну так что, мыслей откуда такой бред получился нет?



Офлайн

#4 Дек. 8, 2008 00:00:27

crchemist
От:
Зарегистрирован: 2008-07-09
Сообщения: 379
Репутация: +  0  -
Профиль   Отправить e-mail  

Странности теста...

ZZZ
Ну так что, мыслей откуда такой бред получился нет?
може так?
[crchemist@test ~]$ time ./a.py

real 0m13.697s
user 0m12.291s
sys 0m3.451s
[crchemist@test ~]$ time ./b.py

real 0m13.822s
user 0m12.246s
sys 0m3.775s

[crchemist@test ~]$ python
Python 2.5.2 (r252:60911, Jul 5 2008, 03:54:54)
[GCC 4.3.0 20080428 (Red Hat 4.3.0-8)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> other_progs_time_with_a = (12.291 + 3.451) - 13.697
>>> other_progs_time_with_b = (12.246 + 3.775) - 13.822
>>> other_progs_time_with_a, other_progs_time_with_b
(2.0450000000000017, 2.1990000000000016)
>>>



Офлайн

#5 Дек. 8, 2008 01:13:38

ZZZ
От: Москва
Зарегистрирован: 2008-04-03
Сообщения: 2161
Репутация: +  26  -
Профиль   Адрес электронной почты  

Странности теста...

crchemist
може так?
Нет, не так. Это, конечно, тоже интересно, и несомненно более правильно, но я не могу понять, что в моём втором тесте так повлияло на результат.



Офлайн

#6 Дек. 8, 2008 16:52:17

crchemist
От:
Зарегистрирован: 2008-07-09
Сообщения: 379
Репутация: +  0  -
Профиль   Отправить e-mail  

Странности теста...

ZZZ
что в моём втором тесте так повлияло на результат.
в мене різниця між a.py і b.py в другому тесті тільки 0.02-0.05 секунди
[crchemist@test ~]$ ./a.py
6.86496496201
6.76033306122
[crchemist@test ~]$ ./b.py
6.73659086227
6.74774694443
[crchemist@test ~]$
6.76033306122 - другий тест в першому варіанті
6.74774694443 - другий тест в другому варіанті

різниця - 0.02 секунди в той час як час на інші програми в 100 разів більший і відрізняється в 10 разів більше
>>> other_progs_time_with_a, other_progs_time_with_b
(2.0450000000000017, 2.1990000000000016)



Офлайн

#7 Дек. 8, 2008 16:55:48

crchemist
От:
Зарегистрирован: 2008-07-09
Сообщения: 379
Репутация: +  0  -
Профиль   Отправить e-mail  

Странности теста...

[crchemist@test ~]$ cat a.py
#!/usr/bin/python
# -*- coding: utf-8 -*-

import timeit

def test_func():
"""Тестовая функция."""
x = 1000000
while x > 0:
x -= 1


import threading

def python_thread_test(threads_num=3, test_func=test_func):
"""Тестируем питоньи нити."""

class Thread(threading.Thread):
def run(self):
test_func()

threads = []
for _ in xrange(threads_num):
threads.append(Thread())
for thread in threads:
thread.start()
for thread in threads:
thread.join()

timer = timeit.Timer('python_thread_test(3)',
'from __main__ import python_thread_test')
print timer.timeit(10)
del timer
del threading
del python_thread_test


from PyQt4 import QtCore
app = QtCore.QCoreApplication([])

def PyQt4_thread_test(threads_num=3, test_func=test_func):
"""Тестируем КьюТэшные нити."""

class Thread(QtCore.QThread):
def run(self):
test_func()

threads = []
for _ in xrange(threads_num):
threads.append(Thread())
for thread in threads:
thread.start()
for thread in threads:
thread.wait()

timer = timeit.Timer('PyQt4_thread_test(3)',
'from __main__ import PyQt4_thread_test')
print timer.timeit(10)[crchemist@test ~]$
[crchemist@test ~]$
[crchemist@test ~]$ cat b.py
#!/usr/bin/python
# -*- coding: utf-8 -*-

import timeit

def test_func():
"""Тестовая функция."""
x = 1000000
while x > 0:
x -= 1


from threading import Thread

def python_thread_test(threads_num=3, test_func=test_func):
"""Тестируем питоньи нити."""

threads = []
for _ in xrange(threads_num):
threads.append(Thread(target=test_func))
for thread in threads:
thread.start()
for thread in threads:
thread.join()

timer = timeit.Timer('python_thread_test(3)',
'from __main__ import python_thread_test')
print timer.timeit(10)
del timer
del Thread
del python_thread_test


from PyQt4 import QtCore
app = QtCore.QCoreApplication([])

def PyQt4_thread_test(threads_num=3, test_func=test_func):
"""Тестируем КьюТэшные нити."""

class Thread(QtCore.QThread):
def run(self):
test_func()

threads = []
for _ in xrange(threads_num):
threads.append(Thread())
for thread in threads:
thread.start()
for thread in threads:
thread.wait()

timer = timeit.Timer('PyQt4_thread_test(3)',
'from __main__ import PyQt4_thread_test')
print timer.timeit(10)[crchemist@test ~]$
[crchemist@test ~]$
[crchemist@test ~]$ time ./a.py
6.80931806564
6.78837585449

real 0m13.639s
user 0m12.300s
sys 0m3.287s
[crchemist@test ~]$ time ./b.py
6.83274817467
6.80398392677

real 0m13.678s
user 0m12.176s
sys 0m3.513s
[crchemist@test ~]$
[crchemist@test ~]$ python -c "print 6.80398392677 - 6.78837585449"
0.01560807228
[crchemist@test ~]$ python -c "print (12.300 + 3.287) - 13.639"
1.948
[crchemist@test ~]$ python -c "print (12.176 + 3.513) - 13.678"
2.011
[crchemist@test ~]$ python -c "print 2.011 - 1.948"
0.063
[crchemist@test ~]$



Отредактировано (Дек. 8, 2008 17:00:17)

Офлайн

#8 Дек. 9, 2008 01:11:33

ZZZ
От: Москва
Зарегистрирован: 2008-04-03
Сообщения: 2161
Репутация: +  26  -
Профиль   Адрес электронной почты  

Странности теста...

Хм… Бред какой-то… Сейчас перепроверил – стабильная разница больше секунды. Ладно, это, наверное, у меня глюк какой.
Спасибо.



Офлайн

Board footer

Модераторировать

Powered by DjangoBB

Lo-Fi Version