Get current time and interval

Reference:

http://blog.sina.com.cn/s/blog_7e2ace6a0100xqk5.html

 

 

C++

DWORD GetTickCount();

GetLocalTime(SYSTEMTIME*);

char   buffer[1024];   
   
SYSTEMTIME   s;   
GetLocalTime(&s);   
   
sprintf(buffer,   "%d-%d-%d   %d:%d:%d",   s.wYear,   s.wMonth,   s.wDay,   s.wHour,   s.wMinute,   s.wSecond);   
   
cout<<"Now:"<<buffer<<endl;


C#

TimeSpan ts1 = new TimeSpan(DateTime.Now.Ticks); //获取当前时间的刻度数

//你的代码或者其他操作

TimeSpan ts2 = new TimeSpan(DateTime.Now.Ticks); 
TimeSpan ts = ts2.Subtract(ts1).Duration();//时间差的绝对值,测试你的代码运行了多长时间。

String spanTime = ts.Hours.ToString() + "小时" + ts.Minutes.ToString() + "分" + ts.Seconds.ToString() + "秒"; 
//以X小时X分X秒的格式显示执行时间


 

public interface IState { string Name { get; } Action EnterAction { get; } Action UpdateAction { get; } Action ExitAction { get; } } public class StateMachine<T> where T : IState { private T currentState; private Dictionary<string, IState> stateDict = new Dictionary<string, IState>(); private float updateInterval = 0.1f; private float updateTimer = 0f; public void AddState(T state) { stateDict[state.Name] = state; } public void SetState(string stateName) { if (currentState != null) { currentState.ExitAction(); } currentState = (T)stateDict[stateName]; currentState.EnterAction(); } public void Update() { updateTimer += Time.deltaTime; if (updateTimer >= updateInterval) { if (currentState != null) { currentState.UpdateAction(); } updateTimer = 0f; } } } public class IdleState : IState { public string Name { get { return "Idle"; } } public Action EnterAction { get { return () => { /* Do idle behavior */ }; } } public Action UpdateAction { get { return () => { /* Do idle behavior */ }; } } public Action ExitAction { get { return () => { /* Do idle behavior */ }; } } } public class WalkingState : IState { public string Name { get { return "Walking"; } } public Action EnterAction { get { return () => { /* Do walking behavior */ }; } } public Action UpdateAction { get { return () => { /* Do walking behavior */ }; } } public Action ExitAction { get { return () => { /* Do walking behavior */ }; } } } public class RunningState : IState { public string Name { get { return "Running"; } } public Action EnterAction { get { return () => { /* Do running behavior */ }; } } public Action UpdateAction { get { return () => { /* Do running behavior */ }; } } public Action ExitAction { get { return () => { /* Do running behavior */ }; } } }如何使用
03-11
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值