Welcome, guest | Sign In | My Account | Store | Cart

A simple calculation of small increments to be applied to a variable, given the variable time that has passed since last update, to make a linear increase over time (in units per second).

Python, 41 lines
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#!/usr/bin/env python
# -*- coding: utf-8 -*-

import time
import random

class Increment(object):
    """Class to calculate increment based on units per second"""
    def __init__(self, units_per_second):
        self.UPS = units_per_second
        self.last_time = time.time()
    def __call__(self):
        now = time.time()
        delta_seconds = (now - self.last_time)
        self.last_time = now
        return delta_seconds * self.UPS

def TimedRange(begin, end, seconds):
    units_per_second = (end - begin) / seconds
    return Increment(units_per_second)

if __name__ == '__main__':
    # usage example
    test1 = 0
    test1_increment = Increment(20) # 20 units per second
    test2 = 800
    test2_increment = TimedRange(800, 5, 5) # 0 to 800 in 5 seconds

    begin = time.time()

    while test1 < 100:
        # do other stuff here,
        # I simulate processing time by sleeping random time up to 0.1 second
        time.sleep(random.random() * 0.1)

        # increment variables based on time passed:
        test1 += test1_increment()
        test2 += test2_increment()
        print test1, test2

    print "It took about %.2f seconds to get test1 from 0 to 100." % (time.time() - begin)

Just playing around with stuff. There is certainly room for improvement.