如何使用MT模板语言计时处理代码块? [英] How can I time processing of chunk of code using MT template language?

查看:77
本文介绍了如何使用MT模板语言计时处理代码块?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通常在开发Movable Type模板时,我想出了多种方法来生成相同的结果,并且很好奇这是更有效的.或者,我只是想知道花费了多长时间,例如生成搜索结果.

Often when developing a Movable Type template, I come up with multiple ways to generate the same result, and am curious which is more efficient. Or, I simply want to know how long something took, such as generating search results.

是否有一种简便的方法可以使用模板标签来记录处理时间或其他计时策略,而无需外部工具?

Is there an easy way to recording processing time or other timing strategies using template tags without requiring external tools?

推荐答案

这是我想出并已经使用了一段时间的方法.它使用标准的Movable Type模板语言将时间报告到最接近的秒数.

This is the method I came up with and have been using for some time. It reports timing to the nearest second, making use of standard Movable Type template language.

要计时一大堆模板代码,请创建一个称为"timing"的系统或博客级别的模板模块:

To time a chunk of template code, create a system or blog level template module called "timing":

<mt:If name="part" eq="start">
  <$mt:Date format="%H" setvar="hours"$>
  <$mt:Date format="%M" setvar="minutes"$>
  <$mt:Date format="%S" setvar="seconds"$>
  <$mt:Var name="hours" op="*" value="3600" setvar="hourseconds"$>
  <$mt:Var name="minutes" op="*" value="60" setvar="minuteseconds"$>
  <$mt:Var name="totalseconds" value="$hourseconds"$>
  <$mt:Var name="totalseconds" op="+" value="$minuteseconds" setvar="totalseconds"$>
  <$mt:Var name="totalseconds" op="+" value="$seconds" setvar="totalseconds"$>
  <$mt:Var name="totalseconds" setvar="startseconds"$>
<mt:Else name="part" eq="stop">
  <$mt:Date format="%H" setvar="hours"$>
  <$mt:Date format="%M" setvar="minutes"$>
  <$mt:Date format="%S" setvar="seconds"$>
  <$mt:Var name="hours" op="*" value="3600" setvar="hourseconds"$>
  <$mt:Var name="minutes" op="*" value="60" setvar="minuteseconds"$>
  <$mt:Var name="totalseconds" value="$hourseconds"$>
  <$mt:Var name="totalseconds" op="+" value="$minuteseconds" setvar="totalseconds"$>
  <$mt:Var name="totalseconds" op="+" value="$seconds" setvar="totalseconds"$>
  <$mt:Var name="totalseconds" setvar="finishseconds"$>
  <$mt:Var name="finishseconds" op="-" value="$startseconds" setvar="elapsedseconds">
  <!-- This search completed in <mt:If name="elapsedseconds" eq="0">less than 1 second<mt:Else name="elapsedseconds" eq="1">1 second<mt:Else><$mt:Var name="elapsedseconds"$> seconds</mt:If>. -->
</mt:If>

然后,在您要用于计时的模板中,将这两行放在感兴趣的代码块的开头和结尾:

Then, in a template you want to time something in, place these two lines at the start and end of the chunk of interest:

<$mt:Include module="timing" part="start"$>
  <mt:Ignore>Code I want to time</mt:Ignore>
<$mt:Include module="timing" part="stop"$>

如果要在输出中表示开始计时的位置,当然可以在开始"部分添加一行输出.

You could of course add a line of output in the "start" section if you want to denote in the output where the timing started.

这篇关于如何使用MT模板语言计时处理代码块?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆