Python Forum
[PyQt] Convertion of float to str is very slow.
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[PyQt] Convertion of float to str is very slow.

I'm making this software in python using pyqt to read a big text file(+60000 lines) with paths to other files. The content of this file is something like this:
493.0 <#> /media/backup/programming/python/fiction - edgar allan poe - the black cat.txt
I'm reading these lines and storing the data in tuples(score(float), filename) because I need.
(493.0, /media/backup/programming/python/fiction - edgar allan poe - the black cat.txt)
When this software starts it shows in a QTableWidget all lines correctly, but when I press buttonSort the convertion of float to str gets heavier and heavier. I would like to know why this is happening only in the second time "showTales()" is called and to fix it.

from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import QTableWidget, QTableWidgetItem

import file

class Ui_Form(object):

	def __init__(self):
		self.file = file.File()
		self.tales = self.file.loadTalesTuples() #It will contains +60 000 tuples

	def setupUi(self, Form):
		self.tableTales = QtWidgets.QTableWidget(Form)
		item = QtWidgets.QTableWidgetItem()
		self.tableTales.setHorizontalHeaderItem(0, item)
		item = QtWidgets.QTableWidgetItem()
		self.tableTales.setHorizontalHeaderItem(1, item)
		item = QtWidgets.QTableWidgetItem()
		self.tableTales.setHorizontalHeaderItem(2, item)
		self.gridLayout.addWidget(self.tableTales, 4, 0, 1, 4)
		self.tableTales.setColumnWidth(1, 200)

		header = self.tableTales.horizontalHeader()
		header.setSectionResizeMode(0, QtWidgets.QHeaderView.ResizeToContents)
		header.setSectionResizeMode(2, QtWidgets.QHeaderView.Stretch)

		self.buttonSort = QtWidgets.QPushButton(Form)
		self.buttonSort.setMinimumSize(QtCore.QSize(80, 23))
		self.buttonSort.setMaximumSize(QtCore.QSize(80, 23))
		self.gridLayout.addWidget(self.buttonSort, 6, 3, 1, 1)


	def sortLinesReverse(self):
		self.tales.sort(key=lambda tup: tup[0], reverse = True)

	def showTales(self, tales):

		line = 0
		for tale in tales:
			#print(tale[0] + " " + tale[1]) To see in terminal. Getting slower after 100 lines when I press buttonSort.
			#self.tableTales.setItem(line, 0, QTableWidgetItem("test")) Works fine the problem probably is in str()
			self.tableTales.setItem(line, 0, QTableWidgetItem(str(tale[0])))
			self.tableTales.setItem(line, 1, QTableWidgetItem(tale[1]))
			line += 1
You can run your script with pycallgraph wrapper to see which function is the bottleneck.

Or use a timer like this;

from functools import wraps
from time import time
def timing(f):
    def wrap(*args, **kw):
        ts = time()
        result = f(*args, **kw)
        te = time()
        print('func:%r args:[%r, %r] took: %2.4f sec' % (f.__name__, args, kw, te-ts))
        return result
    return wrap

def ...():
Okay perhaps not directly related to your question but you might find this handy instead of converting and then re-converting your double value -- store the double value, string value, and 'the rest' that is assuming you actually need to use that double value -- otherwise leave the double as a string and work with it that way. Note if you plan to do a lot of data conversion for any reason you should consider doing that conversion once and then storing both values that way once you have done that conversion its done and eliminating all those extra conversions will greatly speed up your program as a whole and these days memory is cheap.

Forum Jump:

User Panel Messages

Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020