lua中统计毫秒精度的时间

lua自带的时间函数只能到秒的精度。

为了统计到毫秒精度的时间,可以使用luasocket。下载地址http://files.luaforge.net/releases/luasocket/luasocket

编译安装的时候,你可能需要在源码包根目录下的config文件中指定LUAINC变量为你的lua路径。

local socket = require "socket"
local t0 = socket.gettime()
-- do something
local t1 = socket.gettime()
print("used time: "..t1-t0.."ms")

 

update:

如果对精度的要求不需要到毫秒级别,可以用自带的os模块.精度为0.01秒

local s = os.clock()
local e = os.clock()
print("used time"..e-s.." seconds")

Posted by Debug 2013年4月07日 11:44


lua中使用json

用lua的cjson包就行了。

下载地址在这里 http://www.kyne.com.au/~mark/software/lua-cjson.php

安装的话,make&make install就行了。

local cjson = require("cjson")

local str = '["a", "b", "c"]'
local j = cjson.decode(str)
for i,v in ipairs(j) do
    print(v)
end

str = '{"A":1, "B":2}'
j = cjson.decode(str)
for k,v in pairs(j) do
    print(k..":"..v)
end
j['C']='c'
new_str = cjson.encode(j)
print(new_str)

Posted by Debug 2013年3月21日 13:55


用lua访问http

首先你需要lua, 这自不必说。

然后你需要luasocket。 下载地址http://files.luaforge.net/releases/luasocket/luasocket

不过luasocket好像很久没有更新了,也只支持到lua5.1。

local http = require("socket.http")
local ltn12 = require("ltn12")

function http.get(u)
   local t = {}
   local r, c, h = http.request{
      url = u,
      sink = ltn12.sink.table(t)}
   return r, c, h, table.concat(t)
end

url = "http://www.baidu.com"
r,c,h,body=http.get(url)
if c~= 200 then
    return
end
print(body)

Posted by Debug 2013年3月21日 13:45


crontab中多任务执行

首先比较直观是的是把比较复杂的逻辑实现在一个shell脚本,然后在crontab中引用他

echo 'hello world 1';
echo 'hello wordl 2'
* * * * * /path/to/my_script.sh

第二个办法就是直接在crontab配置脚本里面写

* * * * * echo 'hello world 1'; echo 'hello world 2'

值得一说的是,这里的分隔符除了';' 以外,还可以用'&&'和'||', 他们的作用分别是

  • ‘;’:逐条执行命令
  • ‘&&’: 逐条执行,遇到不成功的命令,就中止,后面的不再执行。
  • ‘||’: 逐条执行,遇到成功的命令,就终止,后面的不再执行。

Posted by Debug 2013年3月11日 11:17


如何实现在屏幕上刷新同一行日志

利用回车\r符号,可以回到行首,覆盖掉原来的日志即可。

#include <unistd.h>
#include <stdio.h>
int main()
{
    for(int i=0; i<100; ++i){
        sleep(1);
        printf("\rprint %d times", i);
    }
    return 0;
}

Posted by Debug 2011年10月14日 15:54


Tornado网络框架入门

Tornado是脸书开源的一个轻量级,高效率,非阻塞的python实现的web框架。

我关注它的使用,也就是开发效率。

使用Tornado要求Python2.5以上版本。 如果用到了tornado.httpclient,还必须安装pycurl。如果是使用Python2.5,需要安装simplejson,更高版本就不用了。

使用Tornado开发,不用安装(当然,你也可以按照说明文档安装), 只需要将源码包解压后放在某一个目录,比如/path/to/tornado, 然后在每个用到Tornado的python文件靠前的位置写上

import sys
sys.path.append('/path/to/tornado')

就可以了。

下面是官方网站提供的一个hello world的例子:

#!/usr/bin/env python
import sys
sys.path.append('/path/to/tornado')
import tornado.httpserver
import tornado.ioloop
import tornado.web

class MainHandler(tornado.web.RequestHandler):
    def get(self):
        self.write("Hello, world")

application = tornado.web.Application([
    (r"/", MainHandler),
])

if __name__ == "__main__":
    http_server = tornado.httpserver.HTTPServer(application)
    http_server.listen(8888)
    tornado.ioloop.IOLoop.instance().start()

 

Posted by Debug 2011年2月23日 06:46


三个firefox扩展

delicious for firefox, google notebook for firefox, google calender notifier for firfox这个三个扩展是我非常喜欢用的,但是其中两个已经不再更新,所以不兼容最新的firefox。另外一个不满足我自己的需求,所以也改了两句代码。

Posted by Debug 2010年3月13日 05:16


静态类成员变量初始化顺序

在处理静态类成员变量初始化顺序的问题上,一定要小心

  1. 尽量不要在静态类成员变量之间形成依赖。
  2. 如果一定要有依赖,注意在写Makefile时注意链接object文件的顺序。 被依赖的object文件放在后面。

Posted by Debug 2010年1月22日 01:41


VC编译c程序的问题

一个syntax error : missing ';' before 'type'的编译错误,搞了好久。 原来vc在编译c程序时,一定要把变量的定义放在函数的最前面,一定不能放在某个函数的调用后面。

Posted by Debug 2009年12月23日 07:55


Linux下find命令的几个应用

linux下批量删除某一类文件,以删除当前文件夹下jpg文件为例

find ./ -type f -name “*.jpg” -exec rm {} \;

批量复制某一类文件,一复制当前文件夹下jpg文件到/des_dir为例 

find ./ -type f -name “*.jpg” -exec cp {} /des_dir \;

linux下统计某一个目录下有多少文文件:
综合利用find和wc -l. wc -l 是统计输出命令的行数 

find ./ -type f -maxdepth 1 | wc -l
其中maxdepth表示递归的层数。为1就只统计当前目录。没有指定maxdepth会递归统计子文件夹中的文件

 

Posted by Debug 2009年12月08日 21:28