{"action":"opened","issue":{"url":"https://api.github.com/repos/KehaoWu/SimBayes/issues/1","labels_url":"https://api.github.com/repos/KehaoWu/SimBayes/issues/1/labels{/name}","comments_url":"https://api.github.com/repos/KehaoWu/SimBayes/issues/1/comments","events_url":"https://api.github.com/repos/KehaoWu/SimBayes/issues/1/events","html_url":"https://github.com/KehaoWu/SimBayes/issues/1","id":110522398,"number":1,"title":"性能优化：不使用all","user":{"login":"KehaoWu","id":10395054,"avatar_url":"https://avatars.githubusercontent.com/u/10395054?v=3","gravatar_id":"","url":"https://api.github.com/users/KehaoWu","html_url":"https://github.com/KehaoWu","followers_url":"https://api.github.com/users/KehaoWu/followers","following_url":"https://api.github.com/users/KehaoWu/following{/other_user}","gists_url":"https://api.github.com/users/KehaoWu/gists{/gist_id}","starred_url":"https://api.github.com/users/KehaoWu/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/KehaoWu/subscriptions","organizations_url":"https://api.github.com/users/KehaoWu/orgs","repos_url":"https://api.github.com/users/KehaoWu/repos","events_url":"https://api.github.com/users/KehaoWu/events{/privacy}","received_events_url":"https://api.github.com/users/KehaoWu/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":0,"created_at":"2015-10-08T19:13:30Z","updated_at":"2015-10-08T19:13:30Z","closed_at":null,"body":"在使用 python -m cProfile sample.py对200个SNP进行测试时发现计算时间需要整体计算时间需要16s。在查看性能后发现:\r\n\r\n>\r\n  1465902    0.771    0.000    4.078    0.000 {method 'all' of 'numpy.ndarray' objects}\r\n  2060088    3.359    0.000    3.359    0.000 {method 'join' of 'str' objects}\r\n\r\n\r\nall方法需要比较多的时间。查看代码：\r\n\r\n        for i in range(genodat.shape[0]):\r\n            if (genodat[i,(n-2):n] == ['1','1']).all():\r\n                unit['11'] += 1\r\n            elif (genodat[i,(n-2):n] == ['1','2']).all():\r\n                unit['12'] += 1\r\n            elif (genodat[i,(n-2):n] == ['2','1']).all():\r\n                unit['21'] += 1\r\n            else:\r\n                unit['22'] += 1\r\n\r\n因为矩阵与list比较后返回的是矩阵，需要再用all方法进行与计算。所以改为先用join合并字符，再进行比较和统计：\r\n\r\n        for i in range(genodat.shape[0]):\r\n            hap = \"\".join(genodat[i,(n-2):n])\r\n            if  hap is \"11\":\r\n                unit['11'] += 1\r\n            elif hap is \"12\":\r\n                unit['12'] += 1\r\n            elif hap is \"21\":\r\n                unit['21'] += 1\r\n            else:\r\n                unit['22'] += 1\r\n\r\n\r\n>\r\n  2553376    4.556    0.000    4.556    0.000 {method 'join' of 'str' objects}\r\n\r\n修改后的join操作时间是增加了1s.但是实际计算时间却不一样：\r\n\r\n>\r\nLoad data from  test.ped\r\nbegin to parse data ...\r\nNumber of SNPs:  400\r\nNumber of Subjects will be simulated:  100\r\nInit simulated matrix ...\r\nBegin to simulating at  Thu Oct  8 13:40:05 2015\r\n0 / 197 , time elapse  0.0136458873749  seconds.\r\n100 / 197 , time elapse  8.33574795723  seconds.\r\nEnd at  Thu Oct  8 13:40:21 2015\r\nIt takes about  16.1020810604  seconds.\r\n\r\n修改后\r\n>\r\nLoad data from  test.ped\r\nbegin to parse data ...\r\nNumber of SNPs:  400\r\nNumber of Subjects will be simulated:  100\r\nInit simulated matrix ...\r\nBegin to simulating at  Thu Oct  8 13:48:28 2015\r\n0 / 197 , time elapse  0.0110368728638  seconds.\r\n100 / 197 , time elapse  5.41315102577  seconds.\r\nEnd at  Thu Oct  8 13:48:38 2015\r\nIt takes about  10.358536005  seconds.\r\n\r\n每100SNP时间节省3s，时间减少为原来的65%。"}}