当前位置: 首页 > news >正文

网站开发外包公司坑怎样在网站上做办公家具

网站开发外包公司坑,怎样在网站上做办公家具,网站建设几种语言对比,上海柘中建设股份有限公司网站(二) python 继承和多态这非常类似C的功能#xff0c;只不过是是在C基础上开发的。由上一节知#xff0c;python的所有对象的基础都是PyObject#xff0c;所以例如创建一个PyIntObject对象#xff0c;是通过PyObejct*变量来维护#xff0c;所以在python内部各个函数之间传…(二) python 继承和多态这非常类似C的功能只不过是是在C基础上开发的。由上一节知python的所有对象的基础都是PyObject所以例如创建一个PyIntObject对象是通过PyObejct*变量来维护所以在python内部各个函数之间传递的都是一种范型指针PyObject* 是不是很像C里面的基类。如果要Print(PyIntObject* )由多态(polymophism)我们会知道调用的实际上是PyIntObject对象对应的类型对象中定义的输出操作。看代码longPyObject_Hash(PyObject*v) //注意是PyObject{PyTypeObject*tp v-ob_type; //找到类型if (tp-tp_hash !NULL)return (*tp-tp_hash)(v); //调用相应类型的hash函数/*To keep to the general practice that inheriting* solely from object in C code should work without* an explicit call to PyType_Ready, we implicitly call* PyType_Ready here and then check the tp_hash slot again*///为了维持在C代码单继承中不直接调用PyType_Ready这一惯例在这里间接地调用PyType_Ready()并再次检查tp_hash槽if (tp-tp_dict NULL) {if (PyType_Ready(tp) 0)return -1;if (tp-tp_hash !NULL)return (*tp-tp_hash)(v);}if (tp-tp_compare NULL RICHCOMPARE(tp) NULL) {return _Py_HashPointer(v); /*Use address as hash value*/ //把地址作为hash值返回}/*If theres a cmp but no hash defined, the object cant be hashed*///如果有cmp但是hash没有被定义返回这个对象不能被hashreturnPyObject_HashNotImplemented(v);}以PyIntObject为例观察其实现过程。1 [intobject.h]2 typedef struct{3 PyObject_HEAD4 longob_ival;5 } PyIntObject;67 [intobject.c]8 static PyObject * //注意这里是静态函数而且是PyObject的指针这个是多态的典型特征9 int_add(PyIntObject *v, PyIntObject *w) //加10 {11 register longa, b, x;12 CONVERT_TO_LONG(v, a);13 CONVERT_TO_LONG(w, b);14 /*casts in the line below avoid undefined behaviour on overflow*/15 x (long)((unsigned long)a b);16 if ((x^a) 0 || (x^b) 0)17 returnPyInt_FromLong(x);18 return PyLong_Type.tp_as_number-nb_add((PyObject *)v, (PyObject *)w);19 }2021 static PyObject *22 int_sub(PyIntObject *v, PyIntObject *w) //减23 {24 register longa, b, x;25 CONVERT_TO_LONG(v, a);26 CONVERT_TO_LONG(w, b);27 /*casts in the line below avoid undefined behaviour on overflow*/28 x (long)((unsigned long)a -b);29 if ((x^a) 0 || (x^~b) 0)30 returnPyInt_FromLong(x);31 return PyLong_Type.tp_as_number-nb_subtract((PyObject *)v,32 (PyObject *)w);33 }3435 static PyObject *36 int_mul(PyObject *v, PyObject *w) //乘37 {38 longa, b;39 long longprod; /*a*b in native long arithmetic*/40 double doubled_longprod; /*(double)longprod*/41 double doubleprod; /*(double)a * (double)b*/4243 CONVERT_TO_LONG(v, a);44 CONVERT_TO_LONG(w, b);45 /*casts in the next line avoid undefined behaviour on overflow*/46 longprod (long)((unsigned long)a *b);47 doubleprod (double)a * (double)b;48 doubled_longprod (double)longprod;4950 /*Fast path for normal case: small multiplicands, and no info51 is lost in either method.*/52 if (doubled_longprod doubleprod)53 returnPyInt_FromLong(longprod);5455 /*Somebody somewhere lost info. Close enough, or way off? Note56 that a ! 0 and b ! 0 (else doubled_longprod doubleprod 0).57 The difference either is or isnt significant compared to the58 true value (of which doubleprod is a good approximation).59 */60 {61 const double diff doubled_longprod -doubleprod;62 const double absdiff diff 0.0 ? diff : -diff;63 const double absprod doubleprod 0.0 ?doubleprod :64 -doubleprod;65 /*absdiff/absprod 1/32 iff66 32 * absdiff absprod -- 5 good bits is close enough*/67 if (32.0 * absdiff absprod)68 returnPyInt_FromLong(longprod);69 else70 return PyLong_Type.tp_as_number-nb_multiply(v, w);71 }72 }73由此可知python的int 实际上是C里面的long实现所以加减乘除都是用long实现又由于PyIntObject为一个Immutable对象这个对象不可改变因此在最后return的都是新的对象PyInt_FromLong(num)即由long变量创建一个int变量。(三)python整数的实现在整数里python分为大整数和小整数为了加快计算节省内存的分配时间因为不论是什么对象只要在堆上申请空间是非常费时的所以在涉及到频繁的内存操作时需要做一些优化。python提供了一种比较原始的方法——设个阈值无语了有这么来的么最起码来个动态阈值也好啊...1 #ifndef NSMALLPOSINTS2 #define NSMALLPOSINTS 2573 #endif4 #ifndef NSMALLNEGINTS5 #define NSMALLNEGINTS 56 #endif 范围设定到(-5~257) 在这个区间里面都为小整数7 #if NSMALLNEGINTS NSMALLPOSINTS 08 /*References to small integers are saved in this array so that they //保存在数组里面被共享9 can be shared.10 The integers that are saved are those in the range11 -NSMALLNEGINTS (inclusive) to NSMALLPOSINTS (not inclusive). //[-5, 257)12 */13 static PyIntObject *small_ints[NSMALLNEGINTS NSMALLPOSINTS]; //申请(NSMALLNEGINTS NSMALLPOSINTS)个PyIntObject* 为以后所共享14 #endif15然后是大整数大整数采用块内存区间内缓存1 #define BLOCK_SIZE 1000 /* 1K less typical malloc overhead */2 #define BHEAD_SIZE 8 /* Enough for a 64-bit pointer */3 #define N_INTOBJECTS ((BLOCK_SIZE - BHEAD_SIZE) / sizeof(PyIntObject))45 struct_intblock {6 struct _intblock *next;7 PyIntObject objects[N_INTOBJECTS];8 };910 typedef struct_intblock PyIntBlock;1112 static PyIntBlock *block_list NULL;13 static PyIntObject *free_list NULL;N_INTOBJECTS    到底是多少呢算一下PyIntObject的大小PyIntObject宏展开(without Py_TRACE_REFS)后就是Py_ssize_t  ob_refnt;PyTypeObject *ob_type;long ob_ival;字节大小为448 16 , N_INTOBJECTS   (1000-8)/16 82即一个PyIntBlock维护着的82个PyIntObeject咋一看其实就是个单链表因此这个82个objects相当于是82个数。通过block_list 来维护看代码1 static PyIntObject *2 fill_free_list(void)3 {4 PyIntObject *p, *q;5 /*Pythons object allocator isnt appropriate for large blocks.*/6 p (PyIntObject *) PyMem_MALLOC(sizeof(PyIntBlock));7 if (p NULL)8 return (PyIntObject *) PyErr_NoMemory();9 ((PyIntBlock *)p)-next block_list; //串联blocklist10 block_list (PyIntBlock *)p;11 /*Link the int objects together, from rear to front, then return12 the address of the last int object in the block.*/13 p ((PyIntBlock *)p)-objects[0]; //第一块即front头部注意是地址哦有个“”14 q p N_INTOBJECTS; //从0开始计数q不是尾指针还要减去1才是15 while (--q p) // //最后--q 后即rear尾部从后往前遍历16 Py_TYPE(q) (struct _typeobject *)(q-1); //将所有的PyIntObject 串起来17 Py_TYPE(q) NULL; //q现在为头指针类型置为空18 return p N_INTOBJECTS - 1; //返回rear19 }由上可知相当于对外是一个blocklist, 对内是一系列的PyIntObject当需要重新开辟blocklist时通过((PyIntBlock *)p)-next block_list把这些链表串起来。现在我们可以看看PyInt_FromLong的实现了1 [intobject.c]2 PyObject *3 PyInt_FromLong(longival)4 {5 register PyIntObject *v; //用寄存器操作加快速度6 #if NSMALLNEGINTS NSMALLPOSINTS 07 if (-NSMALLNEGINTS ival ival NSMALLPOSINTS) { //在小数的范围区间直接命中8 v small_ints[ival NSMALLNEGINTS];9 Py_INCREF(v); //引用计数加110 #ifdef COUNT_ALLOCS //这是要统计了11 if (ival 0)12 quick_int_allocs; //正数命中的个数13 else14 quick_neg_int_allocs; //负数命中的个数15 #endif16 return (PyObject *) v;17 }18 #endif19 if (free_list NULL) { //如果是大数且free_list 没有被赋值开始创建20 if ((free_list fill_free_list()) NULL) //这里我们知道freelist指向的是链表的rear21 //和 block_list 是指向PyIntBlock 的指针相区别22 returnNULL;23 }24 /*Inline PyObject_New*/25 v free_list;26 free_list (PyIntObject *)Py_TYPE(v); //强制转换一下类型变成PyIntObject类型27 PyObject_INIT(v, PyInt_Type); //初始化为python的int类型28 v-ob_ival ival;29 return (PyObject *) v;30 }31最后有1 [intobject.c]2 #define PyInt_CheckExact(op) ((op)-ob_type PyInt_Type)3 ...4 static void5 int_dealloc(PyIntObject *v)6 {7 if(PyInt_CheckExact(v)) {8 Py_TYPE(v) (struct _typeobject *)free_list; //如果是整数类对象只是简单的把v置成free_list即空闲链表的起点相当于9 //覆盖的形式10 free_list v;11 }12 else13 Py_TYPE(v)-tp_free((PyObject *)v); //如果不是整数类型调用底层的释放函数14 }1516 static void17 int_free(PyIntObject *v)18 {19 Py_TYPE(v) (struct _typeobject *)free_list; //同上20 free_list v;21 }不要搞混的是上述实现不管是小数还是大数都是起着缓冲池的作用不要误解为是实现大数字功能只不过都是用long实现的这个很容易误导。再看小整数的换冲池1 static PyIntObject *small_ints[NSMALLNEGINTS NSMALLPOSINTS]; //声明为静态指针数组2 .........34 int5 _PyInt_Init(void)6 {7 PyIntObject *v;8 intival;9 #if NSMALLNEGINTS NSMALLPOSINTS 010 for (ival -NSMALLNEGINTS; ival NSMALLPOSINTS; ival) {11 if (!free_list (free_list fill_free_list()) NULL) //同样是申请空闲链表12 return 0;13 /*PyObject_New is inlined*/14 v free_list;15 free_list (PyIntObject *)Py_TYPE(v);16 PyObject_INIT(v, PyInt_Type);17 v-ob_ival ival; //赋值后加入small_ints这个缓冲池18 small_ints[ival NSMALLNEGINTS] v; //相当于一个一一映射的关系19 }20 #endif21 return 1;22 }我们知道的就是说所有的整数都在堆里面有内存小整数使用通过small_ints[]数组一一映射加快查找速度大数则需要通过链表来维护内存。
http://www.yutouwan.com/news/485379/

相关文章:

  • 响应式网站有哪些2017广东企业网站备案
  • 贴心的广州网站建设网站模板的缺点
  • 网站底部广告宝安设计公司
  • 网站建设基本流程网页升级访问网页导航
  • 域名解析后怎么做网站gta 买房网站建设中
  • 网站备案文件推广普通话的广告语
  • 什么是商务网站南昌做网站多少钱
  • 指示灯具网站建设二维码转链接
  • 企业网站系统wordpress织梦哪个好
  • 成品网站w灬源码16伊园网站需要什么费用
  • 太原网站制作哪里便宜建企业网站浩森宇特
  • 石家庄专业做网站深圳注册公司网址
  • 找回老网站seo外包公司优化
  • 网站被k申述钦州做网站
  • 西安个人做网站centos7如何安装wordpress
  • 在别人网站做的友链_为何百度检测带后缀cnindex.asp做网站最贵
  • 专门学设计的网站网站如何做快捷支付接口
  • 大型购物网站建设费用特色的佛山网站建设
  • 广州市口碑好的网站制作排名中国铁路建设工程招标网站
  • 网页设计做军事网站的感想wordpress 后台登陆 修改
  • 网站开发遇到的问题湖北做网站价格
  • wordpress 音乐站主题网站建设费的会计分录
  • 华为弹性云做网站湖南做网站 f磐石网络
  • 建设银行河南分行网站电子商务网站是什么
  • 织梦网站专题页面如何做网站版面布局设计的原则
  • 网站建设内部链接怀宁做网站
  • 什么是网站建设技术个人建个网站多少钱
  • 什么网站用vue做的wordpress首页幻灯片插件
  • 中山优化网站手机网站 html
  • 湖南响应式网站方案做图素材网站开通会员哪个好