stopwatch

在.NET中,“ for”或“ foreach”哪个循环运行得更快?

与世无争的帅哥 提交于 2020-03-16 13:47:50
某厂面试归来,发现自己落伍了!>>> 在C#/ VB.NET / .NET中, for 或 foreach 哪个循环运行得更快? 自从我 很早以前就 读过 for 循环比 foreach 循环的运行速度更快 以来, 我就认为它适用于所有集合,通用集合,所有数组等。 我搜寻了Google并发现了几篇文章,但其中大多数没有定论(阅读文章评论)且开放式。 理想的情况是列出每个方案,并为它们提供最佳解决方案。 例如(仅作为示例): 对于迭代1000+字符串数组- for 好过 foreach 用于遍历 IList (非通用)字符串 foreach 比 for 更好 在网上找到了一些相同的参考资料: Emmanuel Schanzer撰写的原始宏大的旧文章 CodeProject FOREACH与。 对于 博客-要 foreach 或不 foreach ,这是个问题 ASP.NET论坛-NET 1.1 C# for vs foreach [编辑] 除了可读性之外,我对事实和数据真的很感兴趣。 在某些应用程序中,压缩性能优化的最后一步很重要。 #1楼 Jeffrey Richter在TechEd 2005上: “多年来,我已经开始学习C#编译器对我来说是个骗子。” ..“这取决于很多事情。” ..“就像当您执行一个foreach循环时一样...” ..“ ...那是您编写的一小段代码,但是C

C# [HashTable、HashSet和Dictionary的区别]

风流意气都作罢 提交于 2020-03-11 19:39:36
[HashTable、HashSet和Dictionary的区别]   1.HashTable 散列表   哈希表(HashTable)表示键/值对的集合。在**.NET Framework中**,Hashtable是 System.Collections命名空间 提供的一个 容器 ,用于处理和表现类似 key-value的键值对 ,其中key通常可用来快速查找,同时key是区分大小写;value用于存储对应于key的值。Hashtable中key-value键值对均为 object 类型,所以Hashtable可以支持任何类型的key,value键值对,任何非 null 对象都可以用作键或值。   在哈希表中添加一个key/键值对: HashtableObject.Add(key,);    在哈希表中去除某个key/键值对: HashtableObject.Remove(key);   从哈希表中移除所有元素: HashtableObject.Clear();   判断哈希表是否包含特定键key: HashtableObject.Contains(key);    2.HashSet 散列集   HashSet<T>类主要是设计用来做高性能集运算的,例如对两个集合求交集、并集、差集等。集合中包含一组不重复出现且无特性顺序的元素,HashSet拒绝接受重复的对象。  

springboot ftp 笔记

不羁岁月 提交于 2020-03-02 03:47:34
private boolean uploadFile() { try { StopWatch watch = new StopWatch(); watch.start(); // 创建一个ftp对象 FTPClient ftp = new FTPClient(); //ftp.setControlEncoding("GBK"); // ftp连接上去 ftp.connect(ftpHost, ftpPort); // ftp登录上去 ftp.login(ftpUsername, ftpPassword); // 拿到返回码,进行判断是否连接成功 Integer reply = ftp.getReplyCode(); // 连接失败 if (!FTPReply.isPositiveCompletion(reply)) { ftp.disconnect(); return false; } // 连接成功,准备上传 ftp.setFileType(FTPClient.BINARY_FILE_TYPE); // 获取时间节点 Date now = new Date(); SimpleDateFormat formatFile = new SimpleDateFormat("yyyy-MM-dd"); String dateName = formatFile.format(now);

用mybatis插件抓取最终sql

 ̄綄美尐妖づ 提交于 2020-02-27 06:55:05
痛点概述 当我们在排查bug ,需要看执行的完整sql 时,在 console可以拿到如图的sql 然后手工一个一个的替换问号占位符后,去MySQL 执行,看sql有木有什么问题。如果sql简单,那比较好说, 如果是个复杂sql,手动替换N个问号占位符,这种痛相信大家都经历过。 今天介绍的 MybatisFinalSqlPlugin 插件 正是解决了这样的痛点,可以直接抓到最终sql。 环境 mybatis 3.4.2 实现思路 最近在走读mybatis 核心源码时,发现mybatis自己有构建最终sql,就想着把代码抠出来用,后面找到一个切入点,就是通过mybatis 插件机制,在插件里面获取 mybatis 的几大核心对象,然后获取到 mybatis 自己构建的最终sql,大功告成,详细代码如下。 MybatisFinalSqlPlugin 插件 完整代码 package com.anuo.app.common.datalayer.mybatisplugin; import com.google.common.base.Stopwatch; import lombok.extern.slf4j.Slf4j; import org.apache.ibatis.executor.statement.StatementHandler; import org.apache.ibatis

Springboot浅析(二)——容器刷新前的流程

爷,独闯天下 提交于 2020-02-27 06:49:14
大概是水平有限,最近跟读代码与相关书籍感觉巨费时间,想深入弄明白所有的东西很难,所以也只能带着问题来学习springboot了,以后遇到确切的问题再做深入了解把,给自己定个目标,暂时只弄清楚容器启动大体流程,了解组件扫描,自动配置,解决循环依赖这几个问题。 一般启动的Main方法为 SpringApplication.run(启动类.class, args); ,跟下去的话会发现调用的就是new SpringApplication(启动类).run(args)由于容器刷新内容最关键也最复杂,先来了解下容器刷新之前的流程。 (一) SpringApplication的初始化 1.代码 public SpringApplication(ResourceLoader resourceLoader, Class<?>... primarySources) { this.resourceLoader = resourceLoader; Assert.notNull(primarySources, "PrimarySources must not be null"); //通常情况下primarySources就是启动类,暂时理解这里就是将启动类设置为主配置资源来源 this.primarySources = new LinkedHashSet<>(Arrays.asList

3、SpringBoot容器启动整体梳理

Deadly 提交于 2020-02-26 11:11:16
1.1、总体梳理 @SpringBootApplication public class SampleTestApplication { public static void main(String[] args) { SpringApplication.run(SampleTestApplication.class, args); } } public static ConfigurableApplicationContext run(Class<?> primarySource, String... args) { return run(new Class<?>[] { primarySource }, args); } public static ConfigurableApplicationContext run(Class<?>[] primarySources, String[] args) { // 这里分为两步,创建SpringApplication和run方法,primarySources就是主启动类 return new SpringApplication(primarySources).run(args); } 1.2、创建SpringApplication public SpringApplication(Class<?>... primarySources

springboot初探——启动流程

做~自己de王妃 提交于 2020-02-26 06:59:32
前面已经介绍一下springboot,本篇开始介绍springboot在启动过程中做了什么,凭什么那么少的代码就能完成一个web项目。 其他的我们可以先不管,先来看一眼springboot的main方法 @SpringBootApplication public class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } } 这里做了什么?一个注解,一个SpringApplication的静态方法,这两步完成了springboot项目启动的所有步骤。 现在我们来研究到底做了些什么。 注解先不去管它,顺着SpringApplication#run这个方法往下走 public static ConfigurableApplicationContext run(Class<?> primarySource, String... args) { return run(new Class<?>[] { primarySource }, args); } public static ConfigurableApplicationContext run(Class<?>[] primarySources, String[

0216 aop和打印数据库执行日志

扶醉桌前 提交于 2020-02-25 20:34:29
需求 maven依赖 <dependency> <groupId>p6spy</groupId> <artifactId>p6spy</artifactId> <version>3.8.7</version> </dependency> <dependency> <groupId>com.google.guava</groupId> <artifactId>guava</artifactId> <version>28.2-jre</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> <

是DateTime.Now是测量函数性能的最佳方法吗?

好久不见. 提交于 2020-02-25 15:42:54
我需要找到一个瓶颈,并且需要准确地测量时间。 以下代码段是衡量性能的最佳方法吗? DateTime startTime = DateTime.Now; // Some execution process DateTime endTime = DateTime.Now; TimeSpan totalTimeTaken = endTime.Subtract(startTime); #1楼 我刚刚在Vance Morrison的博客中发现了一篇关于他编写 的CodeTimer类的帖子,该类 使得使用 StopWatch 变得更容易并且做了一些整洁的东西。 #2楼 Visual Studio Team System 具有一些可能有助于解决此问题的功能。 基本上,您可以编写单元测试并将它们混合在不同的场景中,以作为压力或负载测试的一部分运行。 这可能有助于确定最能影响应用程序性能的代码区域。 Microsoft的模式和实践组在 Visual Studio Team System性能测试指南中 有一些指导。 #3楼 我在程序中使用的方式是使用StopWatch类,如下所示。 Stopwatch sw = new Stopwatch(); sw.Start(); // Critical lines of code long elapsedMs = sw.Elapsed

Unrecognized selector sent to instance NSTimer Swift

狂风中的少年 提交于 2020-02-13 09:43:52
问题 I'm trying to developing an app that includes a simple Stopwatch feature. I'm using Xcode 6 and the Swift language. Here is the code in FirstViewController @IBAction func Stopwatch (Sender:UIButton) { var startTime = NSTimeInterval() func updateTime(timer:NSTimer) { //Find Current TIme var currentTime = NSDate.timeIntervalSinceReferenceDate() //Find the difference between current time and start time var elapsedTime :NSTimeInterval = currentTime - startTime //calculate the minutes in elapsed