Issue1986

classification
Title: __file__ and hasattr are slow for java packages
Type: Severity: normal
Components: Versions:
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: Vanuan, fwierzbicki, zyasoft
Priority: low Keywords:

Created on 2012-11-07.15:04:28 by Vanuan, last changed 2014-10-06.12:28:10 by zyasoft.

Messages
msg7512 (view) Author: (Vanuan) Date: 2012-11-07.15:04:27
Reproduce:

import sys
import os
import java.io
import time

java_module = sys.modules['java.io']

start = time.time()
filename = java_module.__file__
timeElapsed = round(time.time() - start, 4)
print "java.io: " + str(timeElapsed) + " s"

python_module = sys.modules['os']
start = time.time()
filename = python_module.__file__
timeElapsed = round(time.time() - start, 4)
print "os: " + str(timeElapsed) + " s"


Expected output:

__file__ operation time should be the same for both "os" and "java.io" module

Actual results differ:

java.io: 0.017 s
os: 0.0 s
msg7513 (view) Author: (Vanuan) Date: 2012-11-07.15:09:28
The version is 2.5.3
msg7514 (view) Author: (Vanuan) Date: 2012-11-07.15:21:40
hasattr is also slow for java modules.
msg7515 (view) Author: (Vanuan) Date: 2012-11-07.16:53:57
This is only true for java packages.
msg7518 (view) Author: (Vanuan) Date: 2012-11-08.11:31:02
Here is a more refined example:

import os
import time
import java.lang


def countTime(function):
    start = time.time()
    for _ in range(1, 1000):
        function()
    timeElapsed = round(time.time() - start, 5)
    return timeElapsed


timeElapsed = countTime(lambda: hasattr(java, 'some attr'))
print "hasattr(javapackage): " + str(timeElapsed) + " s"

timeElapsed = countTime(lambda: hasattr(java.lang.System, 'some attr'))
print "hasattr(javapackage.class): " + str(timeElapsed) + " s"

timeElapsed = countTime(lambda: hasattr(os, 'some attr'))
print "hasattr(python module): " + str(timeElapsed) + " s"
History
Date User Action Args
2014-10-06 12:28:10zyasoftsetnosy: + zyasoft
2013-02-26 18:38:33fwierzbickisetpriority: low
nosy: + fwierzbicki
2012-11-08 11:31:03Vanuansetmessages: + msg7518
2012-11-07 16:53:57Vanuansetmessages: + msg7515
title: __file__ and hasattr are slow for java modules -> __file__ and hasattr are slow for java packages
2012-11-07 15:21:40Vanuansetmessages: + msg7514
title: __file__ is slow for java modules -> __file__ and hasattr are slow for java modules
2012-11-07 15:09:28Vanuansetmessages: + msg7513
2012-11-07 15:04:28Vanuancreate