for (int i=0; i<dataArray.count(); i++) { T_departmentInfo departmentInfo; departmentInfo.label.clear();// = dataArray.at(i).toObject().value("label").toString();; departmentInfo.departmentId = dataArray.at(i).toObject().value("departmentId").toString(); departmentInfo.departmentName = dataArray.at(i).toObject().value("departmentName").toString(); departmentInfo.departmentType = dataArray.at(i).toObject().value("departmentType").toInt();; //部门类型 0普通 1独立 departmentInfo.parentId.clear();// = dataArray.at(i).toObject().value("parentId").toString(); departmentInfo.phone.clear();// = dataArray.at(i).toObject().value("phone").toString(); departmentInfo.userId.clear();// = dataArray.at(i).toObject().value("userId").toString(); //空为部门,非空为员工 departmentInfo.userName.clear();// = dataArray.at(i).toObject().value("userName").toString(); departmentInfo.picture.clear();// = dataArray.at(i).toObject().value("picture").toString(); QStandardItem* item = new QStandardItem(departmentPersonModel->rowCount()); departmentPersonModel->appendRow(item); QModelIndex index = departmentPersonModel->indexFromItem(item); PersonItem *personItem = new PersonItem(ui->listView_department_person); personItem->setDepartmentInfo(departmentInfo); ui->listView_department_person->setIndexWidget(index, personItem);

时间: 2024-03-13 14:43:52 浏览: 37
对于这段代码,我们可以进行如下几个方面的优化: 1. 减少重复调用 在这段代码中,dataArray.at(i).toObject() 出现了多次,这样会增加不必要的计算。我们可以将其提取出来,赋值给一个对象,然后多次使用这个对象,从而减少计算量。 2. 优化数据类型 在这段代码中,对于一些字符串类型的变量,如parentId、phone、userId、userName、picture等,都是用clear()方法赋空值,这种方式比较耗费资源。我们可以将这些变量改为QString类型,并且在定义时就赋值为空字符串"",这样就不需要调用clear()方法了。 3. 减少创建对象 在这段代码中,每次循环都会创建一个QStandardItem和一个PersonItem对象,这样会占用大量的内存资源。我们可以将这些对象的创建放到循环外面,并且使用一个指针数组保存这些对象的指针,在循环中重复利用这些对象,从而减少内存的占用。 下面是优化后的代码: ```c++ QJsonObject obj; for (int i = 0; i < dataArray.count(); i++) { obj = dataArray.at(i).toObject(); T_departmentInfo departmentInfo; departmentInfo.departmentId = obj.value("departmentId").toString(); departmentInfo.departmentName = obj.value("departmentName").toString(); departmentInfo.departmentType = obj.value("departmentType").toInt(); //部门类型 0普通 1独立 departmentInfo.parentId = obj.value("parentId").toString(); departmentInfo.phone = obj.value("phone").toString(); departmentInfo.userId = obj.value("userId").toString(); //空为部门,非空为员工 departmentInfo.userName = obj.value("userName").toString(); departmentInfo.picture = obj.value("picture").toString(); PersonItem *personItem = nullptr; QStandardItem *item = nullptr; if (i < itemArray.size()) { item = itemArray.at(i); personItem = static_cast<PersonItem *>(ui->listView_department_person->indexWidget(departmentPersonModel->indexFromItem(item))); } else { item = new QStandardItem(departmentPersonModel->rowCount()); departmentPersonModel->appendRow(item); personItem = new PersonItem(ui->listView_department_person); itemArray.append(item); widgetArray.append(personItem); } item->setData(departmentInfo.departmentId, Qt::UserRole); personItem->setDepartmentInfo(departmentInfo); ui->listView_department_person->setIndexWidget(departmentPersonModel->indexFromItem(item), personItem); } ```

相关推荐

import numpy as np import matplotlib.pyplot as plt import math def count(lis): lis = np.array(lis) key = np.unique(lis) x = [] y = [] for k in key: mask = (lis == k) list_new = lis[mask] v = list_new.size x.append(k) y.append(v) return x, y mu = [14, 23, 22] sigma = [2, 3, 4] tips = ['design', 'build', 'test'] figureIndex = 0 fig = plt.figure(figureIndex, figsize=(10, 8)) color = ['r', 'g', 'b'] ax = fig.add_subplot(111) for i in range(3): x = np.linspace(mu[i] - 3*sigma[i], mu[i] + 3*sigma[i], 100) y_sig = np.exp(-(x - mu[i])**2/(2*sigma[i]**2))/(math.sqrt(2*math.pi)) ax.plot = (x, y_sig, color[i] + '-') ax.legend(loc='best', frameon=False) ax.set_xlabel('# of days') ax.set_ylabel('probability') plt.show() plt.grid(True) size = 100000 samples = [np.random.normal(mu[i], sigma[i], size) for i in range(3)] data = np.zeros(len(samples[1])) for i in range(len(samples[1])): for j in range(3): data[i] += samples[j][i] data[i] = int(data[i]) a, b = count(data) pdf = [x/size for x in b] cdf = np.zeros(len(a)) for i in range(len(a)): if i > 0: cdf[i] += cdf[i - 1] cdf = cdf/size figureIndex += 1 fig = plt.figure(figureIndex, figsize=(10, 8)) ax = fig.add_subplot(211) ax.bar(a, height=pdf, color='blue', edgecolor='white', label='MC PDF') ax.plot(a, pdf) ax.legend(loc='best', frameon=False) ax.set_xlabel('# of days for project') ax.set_ylabel('probability') ax.set_title('Monte Carlo Simulation') ax = fig.add_subplot(212) ax.plot(a, cdf) ax.legend(loc='best', frameon=False) ax.set_xlabel('# of days for project') ax.set_ylabel('probability') ax.grid(True) plt.show()修改一下代码

最新推荐

recommend-type

rockyou.txt

rockyou
recommend-type

ASP+ACCESS网上人才信息管理系统(源代码+论文)【ASP】.zip

ASP+ACCESS网上人才信息管理系统(源代码+论文)【ASP】
recommend-type

河北金融学院经济大数据课设2024年 软科学校爬虫课设

河北金融学院经济大数据课设2024年 软科学校爬虫课设
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

MATLAB图像处理算法宝典:从理论到实战

![MATLAB图像处理算法宝典:从理论到实战](https://img-blog.csdnimg.cn/20200717112736401.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2d1emhhbzk5MDE=,size_16,color_FFFFFF,t_70) # 1. MATLAB图像处理基础理论 MATLAB图像处理是一种利用MATLAB编程语言进行图像处理的强大工具。它提供了丰富的函数和工具箱,用于图像获取、增强、分
recommend-type

matlab中1/x的非线性规划

在MATLAB中,可以使用非线性规划函数(`fmincon`)来优化一个包含1/x的非线性目标函数。下面是一个简单的例子: ```matlab % 定义目标函数 fun = @(x) 1/x; % 定义约束函数(这里没有约束) nonlcon = []; % 定义初始点 x0 = 1; % 定义优化选项 options = optimoptions('fmincon', 'Display', 'iter'); % 进行非线性规划 [x, fval] = fmincon(fun, x0, [], [], [], [], [], [], nonlcon, options); ``` 在
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。
recommend-type

"互动学习:行动中的多样性与论文攻读经历"

多样性她- 事实上SCI NCES你的时间表ECOLEDO C Tora SC和NCESPOUR l’Ingén学习互动,互动学习以行动为中心的强化学习学会互动,互动学习,以行动为中心的强化学习计算机科学博士论文于2021年9月28日在Villeneuve d'Asq公开支持马修·瑟林评审团主席法布里斯·勒菲弗尔阿维尼翁大学教授论文指导奥利维尔·皮耶昆谷歌研究教授:智囊团论文联合主任菲利普·普雷教授,大学。里尔/CRISTAL/因里亚报告员奥利维耶·西格德索邦大学报告员卢多维奇·德诺耶教授,Facebook /索邦大学审查员越南圣迈IMT Atlantic高级讲师邀请弗洛里安·斯特鲁布博士,Deepmind对于那些及时看到自己错误的人...3谢谢你首先,我要感谢我的两位博士生导师Olivier和Philippe。奥利维尔,"站在巨人的肩膀上"这句话对你来说完全有意义了。从科学上讲,你知道在这篇论文的(许多)错误中,你是我可以依
recommend-type

MATLAB数据可视化黑科技:洞悉数据背后的秘密

![MATLAB数据可视化黑科技:洞悉数据背后的秘密](https://img-blog.csdnimg.cn/img_convert/1a36558cefc0339f7836cca7680c0aef.png) # 1. MATLAB数据可视化的基本原理 MATLAB数据可视化是一种将数据转化为图形表示形式的技术,以帮助人们理解和分析数据。其基本原理包括: - **数据表示:**将原始数据转换为图形元素,如点、线和条形。 - **图形布局:**确定图形元素在绘图区域中的位置和大小。 - **图形美化:**添加标题、标签、颜色和纹理等元素,以增强图形的可读性和美观性。 - **交互性:**允