Dino Viehland <dinoviehl...@gmail.com> added the comment:

Setup a micro-benchmark, foo.c:

#define PY_SSIZE_T_CLEAN
#include <Python.h>
#include <string.h>
#include <stdlib.h>

int
main(int argc, char *argv[])
{
    wchar_t *program = Py_DecodeLocale(argv[0], NULL);
    if (program == NULL) {
        fprintf(stderr, "Fatal error: cannot decode argv[0]\n");
        exit(1);
    }
    Py_SetProgramName(program);  /* optional but recommended */
    Py_Initialize();
    PyObject *pName = PyUnicode_DecodeFSDefault("foo");
    if (pName == NULL) { printf("no foo\n"); PyErr_Print(); }
    PyObject *pModule = PyImport_Import(pName);
    if (pModule == NULL) { printf("no mod\n"); PyErr_Print(); return 0; }
    PyObject *cls = PyObject_GetAttrString(pModule, "C");
    if (cls == NULL) { printf("no cls\n"); }
    PyObject *fs[20];
    for(int i = 0; i<20; i++) {
         char buf[4];
         sprintf(buf, "f%d", i);
         fs[i] = PyUnicode_DecodeFSDefault(buf);
    }
    for(int i = 0; i<100000000; i++) {
         for(int j = 0; j<20; j++) {
             if(_PyType_Lookup(cls, fs[j])==NULL) {
                printf("Uh oh\n");
             }
         }
    }

   if (Py_FinalizeEx() < 0) {
        exit(120);
    }
    PyMem_RawFree(program);
    return 0;
}


Lib/foo.py:
import time


class C:
    pass

for i in range(20):
    setattr(C, f"f{i}", lambda self: None)


obj hash: 0m6.222s
str hash: 0m6.327s
baseline: 0m6.784s

----------

_______________________________________
Python tracker <rep...@bugs.python.org>
<https://bugs.python.org/issue43452>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to