Issue1986

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

Created on 2012-11-07.15:04:28 by Vanuan, last changed 2015-04-19.22:54:17 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"
msg9901 (view) Author: Jim Baker (zyasoft) Date: 2015-04-19.22:54:16
Different code paths. It's not nearly as slow now, with rounding removed from the original test:

$ jython27 t-file.py
java.io:  0.000999927520752
os:  0.0

I'm going to close this out - we will want to analyze performance for more than just this one example, just so we put our resources in the right place.
History
Date User Action Args
2015-04-19 22:54:17zyasoftsetstatus: open -> closed
resolution: rejected
messages: + msg9901
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