Thursday, June 30, 2011

python example (jpeg timestamp)

This is a combination usage of:
iterator
callback
yield
image processing

Created/updated by BA43B09F0725,qunshan@newsmth

import os, sys, time
import Image, ImageDraw, ImageFont

def findfiles(path, *callbacks):
for root, dirs, names in os.walk(path):
for name in names:
filename = os.path.join(root, name)
info = tuple(x(filename) for x in callbacks)
yield (filename,)+info

def addString(filename, timestr):
font = ImageFont.truetype(os.path.join(os.environ['windir'], 'Fonts', 'AGENCYB.TTF'), 108)
im = Image.open(filename)
x, y = im.size
draw=ImageDraw.Draw(img)
#draw.ink = 0 + 255*256 + 0*256*256
draw.text((x-640,y-128), timestr, font=font)
img.save(filename)

def addTimeString(filename, mtime):
timestr = time.strftime("%Y-%m-%d %H:%M", time.localtime(mtime))
addString(filename, timestr)
os.utime(filename, (time.time(), mtime))

if __name__ == '__main__':
for filename, mtime in findfiles(sys.argv[1], os.path.getmtime):
if filename.lower().endswith(('.jpg', '.jpeg')):
addTimeString(filename, mtime)

Thursday, June 23, 2011

停车技巧

停车技巧:

非字形停车位:右侧车身 距离车位线1.5m,向后倒车; 在驾驶位位于隔一个停车位正中央时,向内侧打满轮,至车正,回正。

一字形停车: 右侧车身 距离车位线0.5m,向后倒车;在后视镜和前车B柱平行时,向内侧打满轮,继续倒车,至车呈45度角,反向打满轮,至车正,回正。

斜线停车位:后视镜对停车位近端边线时,向外侧打满轮,至车身和车位平行,回正,入车位。

自动档驾驶学习

N档的作用:
转自http://club.autohome.com.cn/bbs/thread-c-413-824998-1.html

1、被拖车时用: (拖行距离不超过50公里)。

2、长时间(超过1分钟至数分钟内)等红灯时用: (在我们这里,红绿灯都有数字递减显示。就可以根据上述原则,采取“①挂N,拉手闸。②保持D档,拉手闸。”)

3、汽车启动、熄火时: (所有汽车说明书都是讲点火要挂在P档,但问题在于: 当在P档点火后,要挂入D档前,是一定要经过R(倒车档)档,当经过R档时,车辆会自然震动一下,这是挂R档的缘故,从P档到D档过程中,等于先入了R档再入D档,无形中多了一次入R档的组合和磨擦,

我的做法是:踩着刹车打开钥匙第一级--挂N档--打火--挂D档--松手刹、放脚刹起步。
自动档熄车也有讲究,我是这样操作的: 熄火前踩着刹车挂N档--拉手刹--放开脚刹--关钥匙第一级--入P档--关钥匙第二级并取出钥匙。 其道理是:停车时,当踩着刹车入P档,拉手刹放开脚刹时,车辆会惯性地向前或向后移动一点位置,这是因为停车场不平整或有少许斜坡而造成的,不要小看这一点点移动,它会对自动变速箱中的锁止栓有一定的冲击,日积月累会对锁止栓有损害,就会出现过锁止栓断开的故障)。

Sunday, June 12, 2011

cache - set associative

Cache Associative:

Full-Associative
If the replacement policy is free to choose any entry in the cache to hold the copy, the cache is called fully associative.
Direct-Mapped
At the other extreme, if each entry in main memory can go in just one place in the cache, the cache is direct mapped.
N-Way-Associative
Many caches implement a compromise in which each entry in main memory can go to any one of N places in the cache, and are described as N-way set associative.

If there are N places to which the replacement policy could have mapped a memory location, then to check if that location is in the cache, N cache entries must be searched. Checking more places takes
*PROs: caches with more associativity suffer fewer misses
*CONs: more power, chip area, and potentially time.


From direct mapped to 2-way, or from 2-way to 4-way, has about the same effect on hit rate as doubling the cache size.
Associativity increases beyond 4-way have much less effect on the hit rate


Example:
An 4K cache is often organized as a set of 256 lines(slots) of 16 bytes each.
Accordingly, a 4-way associative 4K cache has 64 sets, each set having 4 lines mapped.

A 32b address is divided into
[31:10] [9:4] [3:0]
tag set offset

For a given physical addr, first find the set with A[9:4] in cache.
Then search in all 4 slots to see if the tag A[31-10] matches the tag in the slot.