All,
My application code on Windows uses less than 1% CPU time. However,
on Linux (Ubuntu 10.4), my application code uses 11% CPU time. For the
most part, my application code displays text in different colors.
Every 200ms, I need to process up to 15 characters.
Below is a sample program I put together to demonstrate the excessive
CPU usage problem.
What am I do wrong? How can I make my application code use
considerable less CPU time?
Thanks in advance,
Bruce
import wx
import threading
import time
import os
class DisplayThread (threading.Thread):
def __init__ (self, mainw):
threading.Thread.__init__(self)
self.mainw = mainw
self.runFlag = True
def stop (self):
self.runFlag = False
def run (self):
delay = 0.2 #200ms (every 200 ms, upto 15 chars need to be
displayed)
count = 0
while (self.runFlag == True):
for self.ii in range (0,500):
self.colorOutput ("A" , 'red')
self.colorOutput ("B" , 'green')
self.colorOutput ("C" , 'black')
self.colorOutput ("D" , 'orange')
self.colorOutput ("E" , 'blue')
self.colorOutput ("F" , 'red')
self.colorOutput ("G" , 'green')
self.colorOutput ("H" , 'black')
self.colorOutput ("I" , 'orange')
self.colorOutput ("J" , 'blue')
time.sleep (delay)
count = count + 10
if count >= 40:
self.colorOutput ("\n" , 'black')
count = 0
self.stop()
os._exit (1)
def colorOutput (self , text, color):
selection = 0
if selection == 0:
wx.CallAfter (self.mainw.xdisplay,text,color) #Uses
approx 10% CPU time
elif selection == 1:
wx.CallAfter (self.mainw.xxdisplay,text,color) #Uses
approx 4% CPU time
else:
wx.CallAfter (self.mainw.xxxdisplay,text,color) #Uses approx 6%
CPU time
class MyFrame(wx.Frame):
def __init__(self, parent, id, title):
wx.Frame.__init__(self, parent, id, title, wx.DefaultPosition,
size=(500, 500))
panel = wx.Panel(self, -1)
font1 = wx.Font(10, wx.SWISS, wx.NORMAL, wx.NORMAL, False,
u'Courier New')
style = wx.TE_READONLY | wx.TE_RICH2 | wx.TE_MULTILINE |
wx.TE_LEFT
self.text1 = wx.TextCtrl(panel, -1, "", pos=(20,15),size =
(400,300),style = style)
self.text1.SetFont(font1)
self.Center()
self.Show(True)
self.displayThread = DisplayThread (self)
self.displayThread.start()
def xdisplay (self, data, color): #various colors
start = self.text1.GetLastPosition()
self.text1.AppendText (data)
end = self.text1.GetLastPosition()
style = wx.TextAttr(color, None)
self.text1.SetStyle(start, end, style)
def xxdisplay (self, data, color): #black only
self.text1.AppendText (data)
def xxxdisplay (self, data, color): #red only
start = self.text1.GetLastPosition()
self.text1.AppendText (data)
end = self.text1.GetLastPosition()
style = wx.TextAttr('red', None)
self.text1.SetStyle(start, end, style)
application = wx.PySimpleApp()
quicktest = MyFrame(None, -1, "test 1")
application.MainLoop()