12月 02

swift2 tableview 滑动删除

部分主要代码

    @IBOutlet weak var userTableView: UITableView!

    override func viewDidLoad() {
        super.viewDidLoad()
        //注册缓存标示
        refreshControl.addTarget(self, action: "refreshData", forControlEvents: UIControlEvents.ValueChanged)
        refreshControl.attributedTitle = NSAttributedString(string: "松手刷新数据")
        userTableView.addSubview(refreshControl)
        refreshData()
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    override func loadView() {
        super.loadView()
    }
    
    //在本例中,只有一个分区
    func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return 1;
    }

    // 删除部分开始
    //滑动删除必须实现的方法
    func tableView(tableView: UITableView,commitEditingStyle editingStyle: UITableViewCellEditingStyle,
        forRowAtIndexPath indexPath: NSIndexPath) {
            let index = indexPath.row
            self.dataArray.removeAtIndex(index)
            self.userTableView.deleteRowsAtIndexPaths([indexPath],
                withRowAnimation: UITableViewRowAnimation.Top)
    }
    
    //滑动删除
    func tableView(tableView: UITableView, editingStyleForRowAtIndexPath indexPath: NSIndexPath)
        -> UITableViewCellEditingStyle {
            return UITableViewCellEditingStyle.Delete
    }
    
    //修改删除按钮的文字
    func tableView(tableView: UITableView,
        titleForDeleteConfirmationButtonForRowAtIndexPath indexPath: NSIndexPath) -> String? {
            return "删除"
    }
    //删除部分结束
12月 01

golang 1.5.1 协程学习测试

golang的协程和多cpu部分再做个学习测试。算法还是使用http://www.simonzhang.net/?p=2789。
做了三个测试。

一、修改了之前单次循环部分。计算中,使用了break(就是delphi和C里的goto)。统一最后返回值,这样只有一个return。

/*
# Date:        2015-11-19
# Author:      simonzhang
# Web:         www.simonzhang.net
# Email:       simon-zzm@163.com
# -------------------------------
*/

package main

import (
        "strconv"
)

var all int = 1E7 //1E7 = 10^7 = python 10**7

func check(num int) bool{
    status := true
    str:=strconv.Itoa(num)
    var str_len int = len(str)
    var a int=str_len-1
    LOOP:
    for b:=0; b

二、单cpu循环8次,因为测试服务器cpu有8核,这样方便测试多cpu下的效果。

/*
# Date:        2015-11-19
# Author:      simonzhang
# Web:         www.simonzhang.net
# Email:       simon-zzm@163.com
# -------------------------------
*/

package main

import (
        "strconv"
        "runtime"
)

var all int = 1E7 //1E7 = 10^7 = python 10**7

func check(num int) bool{
    status := true
    str:=strconv.Itoa(num)
    var str_len int = len(str)
    var a int=str_len-1
    LOOP:
    for b:=0; b

三、多cpu,每个cpu跑一次。使用协程处理。

/*
# Date:        2015-11-19
# Author:      simonzhang
# Web:         www.simonzhang.net
# Email:       simon-zzm@163.com
# -------------------------------
*/

package main

import (
        "strconv"
        "runtime"
)

var all int = 1E7 //1E7 = 10^7 = python 10**7

func check(num int) bool{
    status := true
    str:=strconv.Itoa(num)
    var str_len int = len(str)
    var a int=str_len-1
    LOOP:
    for b:=0; b=loopnum{
            break
        }
  } 
}

运行结果:
一、
real 0m0.552s
user 0m0.656s
sys 0m0.028s
二、
real 0m4.277s
user 0m5.260s
sys 0m0.096s
三、
real 0m1.434s
user 0m10.605s
sys 0m0.040s

使用协程,多cpu并行处理效果不错。写法简单。第三个测试,因为是多核运行,所以把每核的使用加再一起计算。
10.605秒除8,单个执行速度约为1.324秒,协调所消耗的资源也不小。个人理解,如果运算量比较大还是多进程为好,
前端使用负载进行协调处理。

golangstudy20151127

11月 29

linux 使用tornado的CLOSE_WAIT问题

pypy版本2.6.1
python版本2.7.10
tornado版本4.2.1

用tornado做了个简单的接口,使用pypy后发现大量CLOSE_WAIT。并且无法杀掉。
改动如下
系统/etc/sysctl.conf
net.ipv4.tcp_keepalive_time=60
net.ipv4.tcp_keepalive_probes=2
net.ipv4.tcp_keepalive_intvl=2

nginx的timeout配置设置为30秒。但是没有效果。

使用python后没有这个问题。原因不详。
用callback不知道能不能起作用看到一篇文章
http://www.oschina.net/question/12_17679?sort=time

11月 27

pypy2.6速度测试

主要学习环境
硬件:CPU:Intel(R) Core(TM) i7-4790 CPU @ 3.60GHz
内存:8G
系统:Debian 64位,python2.7.9
加速:pypy2.6.1
下载pypy2.6.1。国内可下,但是速度很慢。
https://bitbucket.org/pypy/pypy/downloads/pypy-2.6.1-src.tar.bz2
解包后放在/usr/local/pypy编译命令如下。
python rpython/bin/rpython -Ojit pypy/goal/targetpypystandalone.py
为了方便调用将命令链接到命令路径中。
ln -s /usr/local/pypy/pypy-c /usr/local/bin/pypy
pypy安装包使用pip管理。下载两个包依顺序用pypy安装
https://pypi.python.org/packages/source/s/setuptools/setuptools-18.3.2.tar.gz#md5=d30c969065bd384266e411c446a86623
https://pypi.python.org/packages/source/p/pip/pip-7.1.2.tar.gz#md5=3823d2343d9f3aaab21cf9c917710196
安装完毕将pypy的pip也链接到系统命令中方便使用
ln -s /usr/local/pypy/bin/pip /usr/bin/pypypip

测试脚本

# -*- coding:utf-8 -*-
# -------------------------------
# Date:        2015-11-27
# Author:      simonzhang
# Web:         www.simonzhang.net
# Email:       simon-zzm@163.com
# -------------------------------

all=xrange(1,10**7)

def check(num):
    a=list(str(num))
    b=a[::-1]
    if a==b:
        return True
    return False

def main():
    for i in all:
        if check(i):
            if check(i**2):
                print i,i**2

测试结果
python:
real 0m6.209s
user 0m6.196s
sys 0m0.008s

pypy:
real 0m1.441s
user 0m1.420s
sys 0m0.016s

结论:
效果pypy加速了4.4倍,在脚本不变的情况下效果明显。该段脚本用C和golang1.5版本都测试过。C和golang耗时
在0.5秒左右。比pypy快3倍。但是从开发速度和代码易理解的程度上说,写小型的需要快速开发的项目,
python还是非常理想的选择。
另两种语言连接http://www.simonzhang.net/?p=2789