[Processing / Python Mode] Lagging when calculate and render every 60 frame per second

Hello there, I am recently started using Processing. I tried to make a line graph visualization, but when I play, The screen goes laggy, without CPU or memory pushing the limit

It goes laggier and laggier when it tries to render another data point.

The data has about 1200 data (date-point) points. Can you guess what line is making my program laggier? Thanks in advance.


import math
from datetime import datetime, timedelta

bars = [["User 1"]]

data = loadTable("data.csv", "csv")
newdata = []
i = 0
while i < 1200:
    li = []
    a = 0
    while a <= 3:
        if a == 0:
            ge = data.getString(i,0)
            fm = datetime.strptime(ge, u'[datetime(%Y/%m/%d)')
            li.append(fm)
        elif a == 1:
            li.append(1)
        elif a == 2:
            ge = data.getString(i,2)
            fm = eval(ge[:-1])
            li.append(fm)
        a += 1
    i += 1
    newdata.append(li)
data = newdata
    
unit = ["", "K", "M", "B", "T", "Qd"]

def format_number(n, type):
    if type == 1 or type == 2:
        if n >= 1000:
            expo = math.floor(math.log(n, 10)+0.000001)
            use_unit = unit[int(math.floor(expo/3))]
            divis = math.floor(float(n)/10**(expo-3+1))/10**((3-expo-1)%3)
            form = '{:,.'+str(int((3-expo-1)%3))+'f}'
            return str(form.format(divis))+use_unit
        else:
            return str(int(math.floor(n)))
    else:
        return '{:,.0f}'.format(n)
    
def get_alpha(a, b, x, type):
    if type == "L":
        return a+(b-a)*x
    
def get_data_from_date(date):
    index = 0
    i = 0
    for d in data:
        if date >= d[0]:
              index = i
        i += 1
    if len(data) > index+1:
        l = data[index+1][0] - data[index][0]
        t = (data[index+1][0] - date)
        x = 1 - t.total_seconds() / l.total_seconds()
        s = data[index][2]
        e = data[index+1][2]
        f = []
        for n in range(0,max(len(s)-1,1)):
            f.append(get_alpha(s[n], e[n], x, "L"))
        return f
    return None

def setup():
    size(1280,720)
    background(0)
    frameRate(60)

padding = (40,40)
num_display = (120,20)
plot = (160,40,1040,660)

process_start_time = datetime.now()
process_start_date = data[0][0]

current_process = data[0][0]
current_index = 0
process_speed = 3 #days
axis_x = timedelta(30)

def draw():
    colorMode( RGB, 255, 255, 255 );
    clear()
    strokeWeight(3)
    stroke(64,64,64)
    line(plot[0],plot[1],plot[0],plot[3])
    line(plot[0],plot[3],plot[2],plot[3])
    
    global current_index, current_process
    if current_process >= data[current_index+1][0]:
        current_index += 1
    index = current_index
    colorMode(HSB, 360,100,100);
    maximum = get_data_from_date(current_process)
    m = 0
    i = 0
    for item in maximum:
        if item >= m:
            m = item
    while index >= 0:
        if not (index >= 0 and axis_x > current_process - data[index][0]):
            try:
                a = (current_process - data[index+1][0]).total_seconds()/axis_x.total_seconds()
                b = (current_process - data[index][0]).total_seconds()/axis_x.total_seconds()
                c = get_data_from_date(data[index+1][0])
                d = get_data_from_date(data[index][0])
                if c != None and d != None:
                    for i in range(0,max(len(c)-1,1)):
                       
                        e = c[i]
                        f = d[i]
                        e_1 = plot[3]-(e/m*620)
                        f_1 = plot[3]-(f/m*620)
                        stroke(360*i/(len(c)),100,100)
                        line(plot[0],f_1+(e_1-f_1)*(b-1)/(b-a),plot[0]+(1-a)*(plot[2]-plot[0]),e_1)
            except IndexError:
                print("End")
            break
        elif index == current_index:
            a = (current_process - data[index+1][0]).total_seconds()/axis_x.total_seconds()
            b = (current_process - data[index][0]).total_seconds()/axis_x.total_seconds()
            c = get_data_from_date(data[index+1][0])
            d = get_data_from_date(data[index][0])
            for i in range(0,max(len(c)-1, 1)):
                e = int(c[i])
                f = int(d[i])
                e_1 = plot[3]-(e/m*620) # index+1
                f_1 = plot[3]-(f/m*620) # index
                stroke(360*i/(len(c)),100,100)
                fill(360*i/(len(c)),100,100)
                line(plot[2]-b*(plot[2]-plot[0]),f_1,plot[2],f_1+(e_1-f_1)*(b/(b-a)))
                textSize(20)
                textAlign(LEFT,BOTTOM)
                text(bars[i][0],plot[2]+10,f_1+(e_1-f_1)*(b/(b-a)))
                textAlign(LEFT,TOP)
                textSize(15)
                text(str(int(math.floor(get_data_from_date(current_process)[i])))+" Subs",plot[2]+10,f_1+(e_1-f_1)*(b/(b-a)))
        else:
            a = (current_process - data[index+1][0]).total_seconds()/axis_x.total_seconds()
            b = (current_process - data[index][0]).total_seconds()/axis_x.total_seconds()
            c = get_data_from_date(data[index+1][0])
            d = get_data_from_date(data[index][0])
            for i in range(0,max(len(c)-1, 1)):
                e = int(c[i])
                f = int(d[i])
                stroke(360*i/(len(c)),100,100)
                line(plot[2]-b*(plot[2]-plot[0]),plot[3]-(f/m*620),plot[2]-a*(plot[2]-plot[0]),plot[3]-(e/m*620))
            

        index -= 1
    current_process = process_start_date + timedelta((datetime.now() - process_start_time).total_seconds() * process_speed)
    textAlign(RIGHT,BOTTOM)
    textSize(50)
    fill(0,0,100)
    text(str(current_process.strftime("%m/%d/%Y")), 1280-padding[0]-(1280-plot[2])-10, 720-padding[1]-num_display[1]-10)

Data looks like this…

[datetime(2017/7/19),1, [1]]
[datetime(2017/7/20),1, [1]]

... 1200 Line of data ...

[datetime(2020/12/22),1, [9184]]
[datetime(2020/12/23),1, [9186]]
1 Like

I’m confused about your sample data. The datetime(2017/7/19) is not Python-syntax; the square-brackets aren’t CSV. What does the console display if you print data[:3] and newdata[:3] just before the data = newdata line?