for和foreach性能比较

TPL5010硬件看门狗电路 在做一些采集频率很低的产品的时候,会考虑电池供电,比如表、电气表等,为了控制功耗,往往会在设备不需要工作的时候让它进入低功耗模式。为了设备的稳定性,避免死机后无法恢复,必须配置看门狗,一般的看门狗芯片,喂狗间隔时间是几秒钟。这样在喂狗和低功耗之间就产生了一个矛盾,频繁的喂狗就需要频繁的醒过来,这样功耗就很难做到很低。这颗看门狗芯片,特别适合上面的应用。2小时的间隔意味着主控芯片每2小时醒过来喂狗一次即可,而在2.5V供电的情况下,功耗仅仅35nA,几乎可以忽略不计。 阅读详情

在很多情况下for和foreach具有同样的功能,选择for还是foreach很多人可能都是看自己的喜好,本测试试图通过 来真是测试数据来比较他们的执行效率。希望能给大家对他们的时候带来一些帮助。

测试环境: 
    硬件环境:   PIII800 + CPU456
    软件环境:   VisualStudio.Net  + C#

测试用例:
    利用结果集来存放记录,初始化对结果集添加记录
    分别对记录数为10000,100000,1000000条记录的时候进行采样分析

    关键测试对比代码如下,功能完全一样:
    //foreach开始时间
   datetime3 = System.DateTime.Now.TimeOfDay.ToString();
   foreach(DataRow row in relationData.Tables[RelationData.RELATIONINFO_TABLE].Rows)
 buffer = row[RelationData.PK_TABLE_NAME].ToString();
   datetime4 = System.DateTime.Now.TimeOfDay.ToString();

   //for开始时间
   datetime5 = System.DateTime.Now.TimeOfDay.ToString();
   for(int j=0;j<1000000;j++)
         buffer = relationData.Tables[RelationData.RELATIONINFO_TABLE].Rows[j][RelationData.PK_TABLE_NAME].ToString();
   datetime6 = System.DateTime.Now.TimeOfDay.ToString();
 
测试结果:
   10000条记录时:
         foreach读取时间:16:29:34.2577584
         foreach结束时间:16:29:34.2677728
         for读取开始时间:16:29:34.2677728
         for读取结束时间:16:29:34.2878016
   100000条记录时:
         foreach读取时间:16:31:10.1055808
         foreach结束时间:16:31:10.1957104
         for读取开始时间:16:31:10.1957104
         for读取结束时间:16:31:10.4460704
   1000000条记录时:
         foreach读取时间:16:33:12.6217504
         foreach结束时间:16:33:13.6332048
         for读取开始时间:16:33:13.6332048
         for读取结束时间:16:33:18.7906208

结果分析:
   1)对于10000条记录可以看出
         foreach用了 0.0100144 
         for循环用了0.0300432
         foreach所花的时间正好是for循环的 1/3
   2)对于100000条记录可以看出
         foreach用了0.0901296  
         for循环用了0.2503600
         foreach所花的时间是for循环的 36%
   3)对于1000000条记录结果可以看出
         foreach用了1.0114544  
         for循环用了4.1574160
         foreach所花的时间是for循环的 25%

    通过对这些测试结果的分析,可以看出相对于原来的for语句foreach具有
更好的执行效率,foreach的平均花费时间只有for30%。通过测试结果在for和foreach都可以使用的情况下,我 们推荐使用效率更高的foreach.在测试同时
我们附加的发现,用for写入数据时间大约是读取数据时间的10倍左右

 
基于QEMU的openEuler aarch64虚拟机快速部署与漏洞验证指南 本文提供了一份基于QEMU在个人电脑上快速部署openEuler aarch64虚拟机的详细指南。内容涵盖从软件准备、目录规划、QEMU安装、虚拟磁盘创建到系统安装与SSH配置的全过程,并以CVE-2024-1086内核漏洞为例,演示了如何在该虚拟环境中进行安全漏洞的验证与补丁测试,为安全研究和运维测试提供了一个低成本、高效率的ARM架构沙箱环境。 阅读详情

相关推荐

Win10 有线网卡配置静态IP后,出现两个IPv4地址的解决方法

在我使用有线网络连接局域网时,配置了静态IP之后,发现网络并没有通,检查之后发现“以太网状态”-“网络连接详细信息”中,出现了2个IPv4的地址(一个是DHCP分配的169.254.xx.xx的IP,另一个是我配置的静态地址),但是网络是没有正常与我所配置的静态地址正常通信的。这个是我执行后的截图,重启电脑后以太网配置静态IP,恢复正常。最终我查了好多网络上的解决办法,有些办法并没有解决我的问题。这个是网上别人的问题截图,我的也是类似这种。第一种(没有解决我的问题)第二种(没有解决我的问题)

Zlz_Nike的博客 2万+

(学习笔记)for循环和forEach性能差异对比

1.写法比较 第一种:普通for循环格式: for(表达式1;循环条件;表达式2){ ...循环体 } 第二种:增强for循环格式: for(类 类的别名 : 类的集合){ ...循环体 } 第三种:iterator 格式: Iterator<Integer> iterator = arrayList.iterator(); while (iterator.hasNext()) { iterator.next(); } 基于增强for实现的扩展写法 forEach:...

qq_35385932的博客 1万+

华为ENSP完整安装教程

华为ENSP完整安装教程

Hnm456756的博客 7330

forEach与for循环的区别(性能与循环跳出)

面试题

WRSR52的博客 2234

基于单片机的自动喷淋系统设计【毕业设计】

随着人民生活质量提高与生活节奏的加快,城市居民对于花草绿植的家庭培养与装饰需求增加,随之因浇灌植物的需要,智能浇花系统的市场逐渐扩大。现有自动浇花系统大部分只能通过设置固定浇水时间而难以实现适时、适量浇水,或在植物环境土壤含水量低时进行报警而并不能实际补水。为克服局限性而设计以单片机为控制中心的自动浇花系统。通过湿度传感器检测目标植物环境土壤湿度数据,再经ADCO832将其放大转换为数字量,由单片机读取输出向LCD1602 显示屏显示并通过比对键盘向单片机所设置的湿度范围而利用中断服务程序进行补水需求判断。

qq_48771816的博客 1711

同济大学824复试全攻略:从笔试到面试的实战解析

本文详细解析同济大学824复试全流程,包括专业课笔试、外语听力与口语、专业外语笔试和专业综合面试的备考策略。重点分享数字信号处理、计算机网络等专业课的高效学习方法,以及专业外语速成和口语面试技巧,助力考生全面提升复试表现。

qq_27496129的博客 1054

刨析 JS 中的forEach、for in、for of三类循环原理和性能

一、for 循环和 while 循环的性能对比 优点:程序简洁,结构清晰,循环初始化,循环变量化,循环体和循环条件位置突出 循环是可以中断循环(利用break语句或return语句)。 缺点:结构比while循环复杂,容易出编码错误 let arr = new Array(999999).fill(1) console.time('forTime') for(let i = 0; i< arr.length; i++){} console.timeEnd('forTi...

muzidigbig的博客 1万+

CTF入门RSA维纳攻击

RSA维纳攻击 一.题目描述 适用于e过大或国过小情况下的RSA加密 题干如下: #coding:utf-8 import gmpy2 import libnum e = 54856417509806712596137531985117125938559627187663765776152288580865785539464797248184444737659643755765127505761012086539564616967122137525108154121304264697865568653100

noj_h的博客 2232

为什么for循环比forEach性能高?

需要注意的是,JavaScript引擎的优化能力不断提升,现代的JavaScript引擎对数组的 forEach 方法可能进行了优化,使其在某些情况下与 for 循环性能相当。循环控制:for 循环对循环的控制更加灵活。可以通过定义循环变量、定义循环条件和更新循环变量的方式来精确控制循环的次数和逻辑,这使得 for 循环在对大型数据集进行迭代时更高效。迭代过程中的中断和跳出:for 循环可以使用 break 语句和 continue 语句在迭代过程中进行中断或跳出,提供了更大的灵活性。

Aan___的博客 898

从仿真到现实:BeamDojo双足机器人项目实战指南(附完整配置清单)

本文提供了一份基于BeamDojo项目的双足机器人从仿真训练到现实部署的完整实战指南。详细拆解了硬件选型(如宇树G1、Jetson Orin NX、Livox Mid-360)、Isaac Gym仿真环境搭建、强化学习训练技巧(如双评论家架构、课程学习)以及关键的Sim2Real迁移策略,旨在帮助开发者将前沿研究成果转化为可落地的工程实践。

hhh00的博客 875

Open3D——点云构建Delaunay三角网【2026最新版】

点云构建Delaunay三角网

点云侠的博客 8367

java中for、foreach(增强for)和stream中foreach的性能和原理

for循环是java出生的时候就已经支持了,在jdk 1.5中开始支持 foreach循环,foreach在一定程度上简化了集合的遍历,但是由于场景的局限性不能完全替代 for 循环。...

延陵缥缈的博客 7516

【Java基础知识 19】Java接口的作用和意义

Java接口是一系列方法的声明,是一些方法特征的集合,一个接口只有方法的特征没有方法的实现,因此这些方法可以在不同的地方被不同的类实现,而这些实现可以具有不同的行为(功能)。

学Java,找哪吒 2万+

Java中for、foreach、stream区别和性能比较

如果数据量在10万的时候,stream效率最高,其次是foreach,最后是for。另外需要注意的是如果数据达到100万的话,parallelStream异步并行处理效率最高,高于foreach和for。stream().forEach 和 forEach 方法都可以使用在集合类上,但是有一些区别。在效率方面,stream().forEach、forEach 和 parallelStream 之间存在一些差异。stream().forEach() 和 forEach() 在使用方式和行为上有一些区别。

刘皇叔说Java的博客 1万+

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

温度PID控制PLC程序

温度PID稳定控制程序,适用于欧姆龙PLC,对其他PLC也具有很大参考价值

Java for循环和foreach循环的性能比较

在公司codereview过程中,发现一个问题,就是有些人循环用的是普通for循环,有些人用的是foreach循环,它们之间有什么区别?应该在什么时候使用这两种循环了? 两种循环的语法格式: 普通for循环语法: for (int i = 0; i < integers.length; i++) { System.out.println(intergers[i]); } foreac

小草的博客 2万+

老版QQ原生表情gif动态

这是老版的QQ表情,不过全是动态的的,Windows照片查看器是不会动的,可以右击用浏览器打开查看,再说一遍是老版的,只是分享给宁愿用老版也要动态的人。

JavaScript 中 for 循环、for...of、forEach、map、filter 和 reduce 的性能比较

for虽然for循环性能最佳,但并不总是最合适的选择。理解每种方法的底层实现和适用场景,才能在实际开发中编写出既高效又优雅的代码。开发优先考虑可读性,关键路径优化性能。

2401_85392853的博客 648

数学建模模型总结

这是本人在两年内准备参加数学建模竞赛搜集到的模型资料,已经经过了本人的筛选和整理,希望能够对准备参加数学建模竞赛的学弟学妹们提供一些帮助!

javascript的map和forEach,for性能比较

for速度比较快,forEach速度和for相当,map慢console.time('map'); for (var i = 0; i < 100000; i++) { var s = new Array(100).fill('1') var arr = s.map((item, idx)=>{ return item; }); } console.time

u012841667的专栏 6797

java foreach和for性能_Foreach循环与For性能比较

java Foreach好还是for()语句好?Foreach循环及For性能及使用上的比较。Foreach语句从什么时候开始有的?JDK1.5起,增加了Foreach语句新功能,Foreach是for循环遍历数据的一种简写形式,关键字依然是for,但参数与格式不同。Foreach其详细用法为:for(Type e:collection){//对语句中变量e的使用}参数说明:e:其类型Type是集...

weixin_32691823的博客 577

console.time和console.timeEnd比较for和forEach的性能

成对使用console.time()和console.timeEnd()比较for 和 forEach谁消耗的时间短: let obj={ name:'Lucy', age:18, gender:'Female' }; let obj2={ name:'Luke', age:18, gender:'Male' } let arr=[]; for(let i=0;i<100;i++){//修改arr的长度 arr.push(obj); } arr.push(obj2).

D_lunar的博客 353

C# 中 for和foreach 性能比较,提高编程性能

在很多情况下for和foreach具有同样的功能,选择for还是foreach很多人可能都是看自己的喜好,本测试试图通过 来真是测试数据来比较他们的执行效率。希望能给大家对他们的时候带来一些帮助。 测试环境: 硬件环境: PIII800 + CPU456 软件环境: VisualStudio.Net + C# 测试用例: 利用结果集来存放记录,初始化对结果集添加记录 分别对记录数

c135205的专栏 1861

java for 和foreach_Java for循环和foreach循环的性能比较

就是有些人循环用的是普通for循环,有些人用的是foreach循环,它们之间有什么区别?应该在什么时候使用这两种循环了?两种循环的语法格式:普通for循环语法:for(inti=0;iSystem.out.println(intergers[i]);}foreach 循环语法:for(Integerin:integers){System.out.println(in);}今天我们...

weixin_39617318的博客 98

C# 中 for和foreach 性能比较

测试环境: 硬件环境: PIII800 + CPU456 软件环境: VisualStudio.Net + C# 测试用例: 利用结果集来存放记录,初始化对结果集添加记录 分别对记录数为10000,100000,1000000条记录的时候进行采样分析 关键测试对比代码如下,功能完全一样: //foreach开始时间 datetime3 = System.DateTime.Now.

刘津宇教编程 2685

for循环和IntStream.forEach()性能比较

1.测试代码 import java.util.stream.IntStream; /** * <p> @Title Test * <p> @Description 测试类 * * @author ACGkaka * @date 2020/4/18 7:13 */ public class Test { /** * 测试for()循环 ...

ACGkaka的博客 2649
上一篇: Using Windows Forms Controls in Internet Explorer
下一篇: for、while、foreach性能比较-PHP篇
forlinux
博客等级 码龄21年 53粉丝 95原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值