帐前卒专栏

code, software architect, articles and novels.
代码,软件架构,博客和小说

node1 node2
nothing

xml 格式如上.

首先要得到root节点:

from xml.etree.ElementTree import ElementTree tree = ElementTree()
tree.parse(filename) # 你想使用哪个文件作为输入? root = tree.getroot()
“”“如果你的输入的是一段content”“” from xml.etree.ElementTree import XML file =
open(filename) content = file.read(-1) root = XML(content) print(root.tag)
file.close() # or you can use from xml.etree.ElementTree import XMLID file =
open(filename) root = XMLID(file.read(-1)) print(root[0].tag) # root[0] is
file.close()

上面的root是class Element的object, 而tree 是ElementTree的object. 如果想得到某个节点的attribute,
例如
则element.attrib[‘name’]即可.如果是text,就是element.text.

如果想得到下一层节点,可以使用root.find(tag)得到,如果不知道具体的tag,那么可以使用root.getchildren().如果想使用某个ta
g作为迭代,例如nodes中有两个node.代码如下:

element_nodes = root.find(‘nodes’) nodes_list =
element_nodes.getiterator(‘node’) for node in nodes_list: print(node.tag)

copy from http://www.cnitblog.com/ictfly/archive/2006/11/25/19594.html

本文只是本人对C++中关于静态类型的一个总结,如错误之处,请大家帮我改正。我分两个方面来总结,第一方面主要是相对于面向过程而言,即在这方面不涉及
到类,第二方面相对于面向对象而言,主要说明static在类中的作用。

一、在面向过程设计中的static关键字

1、 静态全局变量

定义:在全局变量前,加上关键字 static 该变量就被定义成为了一个静态全局变量。

特点:
A、该变量在全局数据区分配内存。
B、初始化:如果不显式初始化,那么将被隐式初始化为0。
C、访变量只在本源文件可见, 严格的讲应该为定义之处开始到本文件结束。

例(摘于C++程序设计教程—钱能主编P103):         //file1.cpp
#include<iostream.h>
void fn();
extern int n;
void main()
{
n=20;
cout << n << endl;
fn();
}

//file2.cpp
#include<iostream.h>

static int n; //定义静态全局变量,初始化为0;
void fn()
{
n++;
cout << n << endl;
}

文件 分别编译能通过,但连接时file1.cpp 中的变量n找不到定义,产生连接错误。

D、文件作用域下声明的const的常量默认 为static存储类型。

2、静态局部变量

定义:在局部变量前加上static关键字时,就定义了静态局部变量。

特 点:
A、该变量在全局数据区分配内存。
B、初始化:如果不显式初始化,那么将被隐式初始化为0。
C、它始终驻 留在全局数据区,直到程序运行结束。但其作用域为局部作用域,当定义它的函数或 语句块结束时,其作用域随之结束。

3、静态函数(注意 与类的静态成员函数区别)

定义:在函数的返回类型前加上static关键字,函数即被定义成静态函数。

特点:
A、静态函数只能在本源文件中使用(这是与普通函数区别)
例(摘于C++程序设计教程—钱能主编P103):         //file1.cpp
void fn();
void staticFn()

void main()
{

fn();
staticFn();
}

//file2.cpp
#include<iostream.h>

static void staticFn();
void fn();

void fn()
{
staticFn();
cout << “this is fn() /n”;
}

void staticFn()
{
cout << “this is staticFn() /n”;
}
连接时,将产生找不到函数 staticFn()定义的错误。

B、主意事项

在文件作用域下声明的inline函数默认为static类 型。

二、面象对象中的static关键字(主要指类中的static关键字)

1、静态数据成员

特 点:
A、内存分配:在程序的全局数据区分配。
B、初始化和定义:
a、静态数据成员定义时要分配空间,所以不 能在类声明中定义。
b、为了避免在多个使用该类的源文件中,对其重复定义,所在,不能在类的头文件中
定义。
c、静态数据成员因为程序一开始运行就必需存在,所以其初始化的最佳位置在类的内部实现。
C、特点
a、对相于 public,protected,private 关键字的影响它和普通数据成员一样,
b、因为其空间在全局数据区分配,属于所有本类
的对象共享,所以,它不属于特定的类对象,在没产生类对象时其作用域就可见,即在没有产生类的实例时,我们就可以操作它。

D、访问 形式
a、 类对象名.静态数据成员名
b、 类类型名:: 静态数据成员名

E、静态数据成员,主要 用在类的所有实例都拥有的属性上。比如,对于一个存款类,帐号相对
于每个实例都是不同的,但每个实例的利息是相同的。所以,应该把利息设为存款类的
静态数据成员。这有两个好处,第一,不管定义多少个存款类对象,利息数据成员都共享分配在全局区的内存,所以节省存贮空间。第二,一旦利息需要改变时,只
要改变一次,则所有存款类对象的利息全改变过来了,因为它们实际上是共用一个东西。

2、静态成员函数

特点:
A、静态成员函数与类相联系,不与类的对象相联系。
B、静态成员函数不能访问非静态数据成员。原因很简单,非静态数据成员属于特定的类实 例。

作用:
主要用于对静态数据成员的操作。
调用形式:
A、类对象名.静态成员函数名()
B、类类型名:: 静态成员函数名()

copy from http://www.javaperformancetuning.com/news/qotm030.shtml

What does

1
volatile
do?

This is probably best explained by comparing the effects that

1
volatile
and
1
synchronized
have on a method.
1
volatile
is a field modifier, while
1
synchronized
modifies code blocks and methods. So we can specify three variations of a simple accessor using those two keywords:

1
2
3
         int i1;              int geti1() {return i1;}
volatile int i2; int geti2() {return i2;}
int i3; synchronized int geti3() {return i3;}
1
geti1()
accesses the value currently stored in
1
i1
in the current thread. Threads can have local copies of variables, and the data does not have to be the same as the data held in other threads. In particular, another thread may have updated
1
i1
in it's thread, but the value in the current thread could be different from that updated value. In fact Java has the idea of a "main" memory, and this is the memory that holds the current "correct" value for variables. Threads can have their own copy of data for variables, and the thread copy can be different from the "main" memory. So in fact, it is possible for the "main" memory to have a value of 1 for
1
i1
, for thread1 to have a value of 2 for
1
i1
and for thread2 to have a value of 3 for
1
i1
if thread1 and thread2 have both updated
1
i1
but those updated value has not yet been propagated to "main" memory or other threads.

On the other hand,

1
geti2()
effectively accesses the value of
1
i2
from “main” memory. A
1
volatile
variable is not allowed to have a local copy of a variable that is different from the value currently held in “main” memory. Effectively, a variable declared
1
volatile
must have it’s data synchronized across all threads, so that whenever you access or update the variable in any thread, all other threads immediately see the same value. Of course, it is likely that
1
volatile
variables have a higher access and update overhead than “plain” variables, since the reason threads can have their own copy of data is for better efficiency.

Well if

1
volatile
already synchronizes data across threads, what is
1
synchronized
for? Well there are two differences. Firstly
1
synchronized
obtains and releases locks on monitors which can force only one thread at a time to execute a code block, if both threads use the same monitor (effectively the same object lock). That’s the fairly well known aspect to
1
synchronized
. But
1
synchronized
also synchronizes memory. In fact
1
synchronized
synchronizes the whole of thread memory with “main” memory. So executing
1
geti3()
does the following:

  1. The thread acquires the lock on the monitor for object
    1
    this
    (assuming the monitor is unlocked, otherwise the thread waits until the monitor is unlocked).
  2. The thread memory flushes all its variables, i.e. it has all of its variables effectively read from "main" memory (JVMs can use dirty sets to optimize this so that only "dirty" variables are flushed, but conceptually this is the same. See section 17.9 of the Java language specification).
  3. The code block is executed (in this case setting the return value to the current value of
    1
    i3
    , which may have just been reset from "main" memory).
  4. (Any changes to variables would normally now be written out to "main" memory, but for
    1
    geti3()
    we have no changes.)
  5. The thread releases the lock on the monitor for object
    1
    this
    .
So where
1
volatile
only synchronizes the value of one variable between thread memory and "main" memory,
1
synchronized
synchronizes the value of all variables between thread memory and "main" memory, and locks and releases a monitor to boot. Clearly
1
synchronized
is likely to have more overhead than
1
volatile
.

貌似网上大家都使用Easytalk搭建微博系统。那东东使用php和mysql的。就是备案难了点,可以考虑放到米国。小量用户速度蛮快的,大量的用户没有测试过.
…(废话ing)有个家伙做了一个IT的微博,就是贴代码麻烦了点,要先去http://pastebin.com那里发布成功,然后再从分享那里贴进来。。。虽然
webservice已经大行其道,但是真正的接口互用似乎还要等到在未来的若干年。

I use c++ in linux which is gcc version 4.4.1 (Ubuntu 4.4.1-4ubuntu9).

I considered that c++ memory alignment is like the following:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
// pseudo-code of memory alignment
remove_all_static_varibles();
int max= 0;
foreach(element in struct)
find_the_max_memory_used(element,&amp;max);
int align = max &gt; default? default: max;
// if max memory used for element is larger than the default, use default.
int memory = sum(memory_used(elements));

if(memory%align != 0)
// here will execute alignment!
{
memory = (memory/align + 1 )*align;
}
if(memory == 0)
memory = 1;
return memory;

vc++ use #pragma_pack(n) set the default.if there are three char in a struct, like:

1
2
3
4
5
6
typedef struct{
char a;
char b;
char c;
}X;
sizeof(X); // it will be 3
1
2
3
4
5
6
7
8
9
10
11
12
13
typedef struct{
char a;
short b;

}Y;
sizeof(Y); // here will be 4

typedef struct{
char a;
int b;
}Z;

sizeof(Z); // here will be 8
1
2
3
4
5
6
7
8
typedef struct{
double a;
char b;
}W;
sizeof(W);
// it will be 12, the align is 4
// when the max memory used is larger
// than 4, it will be 4

if the class or struct is empty, sizeof(it) will be 1.

But if there are static varibles in classes or structs, you can remove it!. like:

1
2
3
4
5
class A{
static int a;

};
sizeof(A); // it will be 1

in class there will be many class functions. But A class is thought as A struct and many functions set. sizeof(A class) will compute the size of struct.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
class ZZ{
public:
ZZ(){
int a = 3;
cout &lt;&lt; "I am ZZ"&lt;&lt;endl;
cout &lt;&lt; a&lt;&lt;endl;
}
~ZZ(){

}
int c;
};
sizeof(ZZ); // will be 4;
/* it is like a struct and two member functions.
struct ZZ{
int c;
};

ZZ@ZZ() is name mingling
ZZ@ZZ(){
int a = 3;
cout &lt;&lt; "I am ZZ"&lt;&lt;endl;
cout &lt;&lt; a&lt;&lt;endl;
}
ZZ@~ZZ(){

}
*/

but when we meet the virtual member functions and virtual derived class, it will be different.

又拆了个本本,这个本本设计的不怎么合理。安装的时候严丝合缝,拆卸的时候就相当的麻烦。首先拧掉所有看得见的螺丝。先拆电源板,要先将屏转到180度,拆掉电源,然
后再拆电源板。拆下电源板时,再拧掉键盘的螺丝和外壳螺丝。这里键盘的螺丝滑了…我就没有继续拆卸…zzz

近期看一个电影<二维平面>(FlatLand),这个电影讲述的是二维平面中发生的故事.发现任何事物都很难脱离自身所在的环境想问题.二维中的square
A就是这样.他只知东西南北四个方向,完全没有向上和向下的概念.所以圆球说线移动成面,面移动成体,对square A来说完全没有概念.但是当square A脱
离了他原本所在的二维平面,他人也就大彻大悟了.如果有一维直线,也就有二维平面,如果有二维平面,也就会有三维立体空间,如果有三维立体空间,也就会存在四维世界.
在二维世界中如果有人宣扬或者知道三维世界,就要被杀掉,因为不利于二维世界的统治.毕竟如果知道还有更高级的生命体或者个体存在,现在的体制就会土崩瓦解.并非出于
惧怕,而是出于信仰.就如同如果神真的存在,那么人还有什么值得崇拜和推崇的?电光火石中争长短,几许光阴;蜗牛角上较雌雄,许大世界?不过真的有四维世界吗?这首先
要论证真的有二维世界和一维世界吗?因为在我们的世界中暂时还找不到二维物体.想多了就晕,即使知道了事实也无法证明.即使证明了也会引来杀身之祸.所以无知是福.觉
得非常奇怪的一点就是知道了就去宣扬,这让我想到日心说.想来哥白尼的确是位智者.当然逼哥白尼临终发表天体运行论的那群人也很聪明.相反布鲁诺多多少少有些愚钝.不
过提点别人的错误,往往自己也抱持相同的错误.最后用白居不易的<对酒>一诗结束此文:蜗牛角上争何事,石火光中寄此身,随贫随福且欢乐,不开口笑是痴人.

** 中 文名 ** : 二维电影

** 英文名 ** : Flatland

** 别名 ** : Flatland the Film

** IMDb ** : 7.0/10 (455 votes)

** 资 源格式 ** : DVDRip

** 发行日期 ** : 2007年

** 导 演 ** : Ladd Ehlinger Jr.

** 演员 ** : Chris Carter … King Of Lineland
Megan Colleen … A Hexagon

Ladd Ehlinger Jr. … A Square

Oscar Gutierrez … Old Trapezoid

Simon Hammond … A Sphere

Michael Karle … Pentagon Doctor

Jeff Sanders … Cube Carlton

Jonathon Shoemaker … Soldier X

Gregory Trent … President Circle

** 地区 ** : 美国

** 语 言 ** : 英语

** 简 介 ** :

[ ](http://image-1.verycd.com/3eb62b5e6eb5aba3cd79530a50037c2845609/post-32595
9-1221031016.jpg)

◎译  名  二维电影
◎片  名 Flatland
◎年  代 2007
◎国  家 美国
◎类  别 动画/奇幻/科幻
◎ 语  言 英语
◎字  幕 N/A
◎IMDB评分 7.4/10 (253 votes)
◎IMDB链接 http://www.imdb.com/title/tt0972374

◎文件格式 XviD + MP3
◎视频尺寸 656 x 272
◎文件大小 1CD 49 x 15MB
◎片  长 99 Mins
◎导  演  Ladd Ehlinger Jr.
◎主  演 Chris Carter … King Of Lineland
Megan Colleen … A Hexagon
Ladd Ehlinger Jr. … A Square
Oscar Gutierrez … Old Trapezoid
Simon Hammond … A Sphere
Michael Karle … Pentagon Doctor
Jeff Sanders … Cube Carlton
Jonathon Shoemaker … Soldier X
Gregory Trent … President Circle

◎简  介

Flatland: The Movie是由1884年一位英国牧师Edwin A.
Abbott(1838-1926)所撰写的一本小册子《Flatland》改编的一部动画片。小说里构造了一个全新的世界──这个世界是二维的!整个小
说分成两个部分,前一部分系统地描述这个二维世界,包括自然状况、居民生活、政治历史等等。真正有趣的事情发生在后一部分里,这里不同维度的世界之间发生
了碰撞——二维世界中的主人公拜访了一维世界,同时又接触到了一个全新的三维世界。当他在他的世界传播三维思想时,整个世界大乱,哥白尼时代的那段故事再 次发生。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#include&lt;cstdio&gt;

using namespace std;
namespace pattern{
/// this pattern links all handler as a line, if previous handler can process a request, it wil not be
/// send to next handler. For example, if someone requested changing programming style,
/// this decision will be made by a programmer.
/// He will change his programming style without noticing his lead.
/// But if someone request changing the requirements, the programmer can not make this decision.
/// He will noticing his lead, and make his lead decided.

/// this is request class.
#define STYLE 0
#define REQIREMENT 1
class Request{
public:
int type; // changing programming style or changing requirements?

};

/// this class for processing request.
class Handler{
private:
Handler* parent;
public:
Handler(Handler* p):parent(p){}
virtual bool DoHandle(Request&amp; r)=0;
void Process(Request&amp; r);
virtual ~Handler(){}
};

void Handler::Process(Request&amp; r){
if(!this-&gt;DoHandle(r) &amp;&amp; NULL != parent){
parent-&gt;Process(r);
}
}

class Programmer: public Handler{
public:
Programmer(Handler* p):Handler(p){}
virtual bool DoHandle(Request&amp; r);

};
bool Programmer::DoHandle(Request&amp; r){
if(r.type == STYLE){
printf("programmer: I can process changing programming style\n");
return true;
}else{
printf("programmer: I can not process this request. I will report this to my lead!\n");
return false;
}

}

class Lead:public Handler{
public:
Lead(Handler* p):Handler(p){}
virtual bool DoHandle(Request&amp; r);

};
bool Lead::DoHandle(Request&amp; r){
if(r.type == REQIREMENT){
printf("lead: I will process changing reqirements\n");
return true;
}
return false;
}
}

int main(){
pattern::Lead lead((pattern::Handler*)NULL);
pattern::Programmer programmer(&amp;lead);
pattern::Request r;
r.type = STYLE;
programmer.Process(r);
r.type = REQIREMENT;
programmer.Process(r);
return 0;

}

在ubuntu或者其他的linux下生成大文件,必须使用编译参数 -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64.否
则生成的文件一定是2G左右的,原因是fseek,这个函数中的offset参数是一个long型的或者是size_t型的,在fseeko中是off_t,但是这个
off_t默认还是32位,只有加了 -D_FILE_OFFSET_BITS=64的宏定义才被作为64位对待.

写了下面一段代码来生成大文件,传说有一种方法:直接跳转nG位置之后,然后写入一个字符.这种方法可能更快,不过我为了保守的生成文件,还是使用下面的一个写法:

#include<stdio.h> #include<stdlib.h> #include<unistd.h> #include<string.h>
void gen_file(const char filename, int block_size, int count){ int is_exist =
0; FILE
file = fopen(filename,“w+b”); char * buf = NULL; int i=0; long long
size = block_size; if(file == NULL){ printf(“fatal error in gen_file:
fopen/n”); exit(1); } buf = (char )malloc(sizeof(char)block_size);
memset(buf,‘1’,sizeof(char)block_size); while(i++<count){ if(i % 10 == 0){
printf(“count is %d, size is %lld/n”,i,size); } fwrite(buf,block_size,1,file);
size += block_size; fseeko(file,size,SEEK_SET); } fclose(file); free(buf); /

long long size = 1024
1024
1024; size =10; fseeko(file,size,SEEK_SET);
fwrite(buf,block_size,1,file); fclose(file); free(buf); / } int main(){
printf(“size of char %d/n”,sizeof(char)); int block_size = 1024
1024
20; //
20M int count = 50; gen_file(“smallfile”,block_size,count*2);// gen 2G file
return 0; }

int block_size = 1024102420; // 20M

这个是每次写入的数据大小,gen_file(“smallfile”,block_size,count*2);第三个参数可以调节生成文件的大小.

下面写了一个测试大文件和小文件的随机读写和顺序读写.

#include<stdio.h> #include<time.h> #include<sys/stat.h> #include<sys/types.h>
#include<unistd.h> #include<stdlib.h> #include<sys/time.h> #include<string.h>
const int block_size = 8092; struct timeval start; struct timeval end; struct
timezone tz; void start_time(){ gettimeofday(&start,&tz); } void end_time(){
gettimeofday(&end,&tz); } void print_time(){ printf(“%lf sec/n”,(end.tv_sec-
start.tv_sec)+(double)(end.tv_usec-start.tv_usec)/1000000); } size_t
look_size(const char * filename){ struct stat st; stat(filename,&st); return
st.st_size; } void read_blocks(FILE* file, off_t offset,int block_size){ int
where = offsetblock_size; fseeko(file,where,0); } FILE fopen_safe(const char

  • filename){ FILE* file = fopen(filename,“r”); if(file == NULL){ printf(“fatal
    error in random_read, open file”); exit(1); } return file; } void
    random_read(const char * filename){ int random; size_t size; FILE* file =
    fopen_safe(filename); size = look_size(filename)/block_size; random =
    rand()%size; read_blocks(file,random,block_size); fclose(file); } void
    order_read(const char filename,int order){ size_t size; FILE file =
    fopen_safe(filename); size = look_size(filename)/block_size;
    read_blocks(file,order%size,block_size); fclose(file); } void
    random_test(const char * filename,int times){ int count = 0; start_time();
    while(count++<times){ // access Big File random_read(filename); } end_time();
    print_time(); } void order_test(const char * filename, int times){ int count =
    0; start_time(); while(count++<times){ order_read(filename,count); }
    end_time(); print_time(); } void random_test_diff(const char * basename,int
    num,int times){ int count = 0; char ** names = malloc(sizeof(char)*num); int
    len = strlen(basename); int i = 0; while( i < num){ names[i] =
    malloc(sizeof(char)*len+4); sprintf(names[i],“%s_%d”,basename,i); i++; }
    start_time(); while(count++<times){ if(count < 900){ random_read(names[0]); }
    else { if(count < 960){ random_read(names[count%num]);
    random_read(names[(count+1)%num]); } if(count < 1000){
    random_read(names[count%num]); random_read(names[(count+1)%num]);
    random_read(names[(count+2)%num]); } } } end_time(); print_time(); } int
    main(){ int i,count; int times = 1000; count = 0; // clock_t start,end;
    srand(time(NULL)); char * filename=“bigfile”; //printf(“time is %lf”,((double
    )(end-start))/CLOCKS_PER_SEC); random_test(filename,times);
    order_test(filename,times); times = times; filename = “smallfile”;
    random_test(filename,times); order_test(filename,times);
    random_test_diff(filename,3,times); return 0; }

当然你这里做测试的时候可以把gettimeofday替换为clock()或者time()函数,其中clock()是程序实际运行的时间,你感觉可能执行了七八秒
,其实程序只占用cpu执行了1秒.如果是time()函数或者gettimeofday()函数就是真正的运行时间也就是你感觉执行的时间.所以各有千秋,前者是为
了排除各种运行环境中的软件或者os的不同,单独检测程序的效率.后者是在系统环境中程序的执行时间.

下面是我得到的结论,创建1个10G文件,创建3个2G小文件.
大文件 随机访问7.199645 sec
大文件 顺序访问0.013617 sec
单个小文件 随机访问5.119245 sec
小文件 顺序访问0.014190 sec
多个小文件的随机访问测试:90%的几率访问同一个小文件,6%访问两个小文件,4%访问3个小文件 9.791248
sec.并且90%几率发生在前,所以如果随机发生多个文件的随机访问,那么可能速度会更慢.这里慢的原因,是因为fopen.

这也就是数据库为什么只有一个数据文件的原因.有时进行文件切分,反而不利于效率的提高.但是对于100G以上的大文件,是否这个结论又要改变了呢?我没有做过这个实
验.

0%