补充性能优化后的函数,注释掉耗时大的函数

main
p6fxi93qh 2 months ago
parent 5bb9032acd
commit 3c2e89689f

@ -158,24 +158,24 @@ class Student(models.Model):
sts.update(probability = (1 - (F('credit') - min_credit)/fm)) sts.update(probability = (1 - (F('credit') - min_credit)/fm))
#耗时最大,性能改进前的代码 #耗时最大,性能改进前的代码
def updateinfor (self,point,class_name,sid,style): #更新积分和概率 # def updateinfor (self,point,class_name,sid,style): #更新积分和概率
if style == 1 : # if style == 1 :
point = point # point = point
if style == 2 : # if style == 2 :
point = point * 2 # point = point * 2
#先改变该学生的积分值 # #先改变该学生的积分值
current_s = Student.objects.filter(class_name=class_name, sid=sid).first() # current_s = Student.objects.filter(class_name=class_name, sid=sid).first()
current_credit = current_s.credit # current_credit = current_s.credit
current_s.credit = current_credit + point # current_s.credit = current_credit + point
current_s.save() # current_s.save()
#查找最新的最大或最小值,与原来写在表中的上次更新的最大值和最小值做对比 # #查找最新的最大或最小值,与原来写在表中的上次更新的最大值和最小值做对比
max_credit = Student.objects.filter(class_name=class_name).aggregate(max_credit=Max('credit'))['max_credit'] # max_credit = Student.objects.filter(class_name=class_name).aggregate(max_credit=Max('credit'))['max_credit']
min_credit = Student.objects.filter(class_name=class_name).aggregate(min_credit=Min('credit'))['min_credit'] # min_credit = Student.objects.filter(class_name=class_name).aggregate(min_credit=Min('credit'))['min_credit']
fm = max_credit - min_credit #最新的分母 # fm = max_credit - min_credit #最新的分母
sts = Student.objects.filter(class_name=class_name) # sts = Student.objects.filter(class_name=class_name)
sts.update(min=min_credit, max=max_credit) # sts.update(min=min_credit, max=max_credit)
# 更新所有学生的概率 # # 更新所有学生的概率
if fm == 0: # if fm == 0:
sts.update(probability=1.0) # sts.update(probability=1.0)
else: # else:
sts.update(probability=(1 - (F('credit') - min_credit) / fm)) # sts.update(probability=(1 - (F('credit') - min_credit) / fm))
Loading…
Cancel
Save