小编Sid*_*Sid的帖子

python日志记录性能比较和选项

我正在研究Python中的高性能日志记录,到目前为止,我们已经对python标准日志记录模块的性能感到失望 - 但似乎没有其他选择.下面是一段性能测试代码4种不同的日志记录方式:

import logging
import timeit
import time
import datetime
from logutils.queue import QueueListener, QueueHandler
import Queue
import threading

tmpq = Queue.Queue()

def std_manual_threading():
    start = datetime.datetime.now()
    logger = logging.getLogger()
    hdlr = logging.FileHandler('std_manual.out', 'w')
    logger.addHandler(hdlr)
    logger.setLevel(logging.DEBUG)
    def logger_thread(f):
        while True:
            item = tmpq.get(0.1)
            if item == None:
                break
            logging.info(item)
    f = open('manual.out', 'w')
    lt = threading.Thread(target=logger_thread, args=(f,))
    lt.start()
    for i in range(100000):
        tmpq.put("msg:%d" % i)
    tmpq.put(None)
    lt.join()
    print datetime.datetime.now() - start

def nonstd_manual_threading():
    start = datetime.datetime.now()
    def logger_thread(f):
        while …
Run Code Online (Sandbox Code Playgroud)

python performance logging multithreading python-multithreading

11
推荐指数
2
解决办法
5032
查看次数

静态多态性与boost变异单访问者与多访问者与动态多态性

我正在比较以下C++多态性方法的性能:

方法[1].静态多态性使用boost变体与每个方法的单独访问者方法[2].静态多态性使用boost变体与单个访问者使用方法重载调用不同的方法方法[3].普通的旧动态多态性

平台: - Intel x86 64位Red Hat现代多核处理器,32 GB RAM - gcc(GCC)4.8.1,带-O2优化 - Boost 1.6.0

一些发现:

  • 方法[1]似乎有明显优于方法[2]和[3]
  • 方法[3]大多数时候都优于方法[2]

我的问题是,为什么我使用访问者但使用方法重载来调用正确方法的方法[2]比虚拟方法提供更差的性能.我希望静态多态性比动态多态更好.我理解在方法[2]中传递的额外参数有一些成本要计算要调用的类的visit()方法,以及由于方法重载可能会有更多的分支?但是,这不应该优于虚拟方法吗?

代码如下:

// qcpptest.hpp

#ifndef INCLUDED_QCPPTEST_H
#define INCLUDED_QCPPTEST_H

#include <boost/variant.hpp>

class IShape {
 public:
  virtual void rotate() = 0;
  virtual void spin() = 0;
};

class Square : public IShape {
 public:
  void rotate() {
   // std::cout << "Square:I am rotating" << std::endl;
    }
  void spin() { 
    // std::cout << "Square:I am spinning" << std::endl; 
  }
};

class Circle : public IShape …
Run Code Online (Sandbox Code Playgroud)

c++ polymorphism performance templates boost

11
推荐指数
1
解决办法
943
查看次数

Pythonic从字典中增加和分配id的方法

这似乎是一种非常常见的模式:

for row in reader:
    c1=row[0]
    if ids.has_key(c1):
        id1=ids.get(c1)
    else:
        currid+=1
        id1=currid
        ids[c1]=currid
Run Code Online (Sandbox Code Playgroud)

我想知道是否有更好的方法来实现这一目标.至于单行if语句,我可以这么做:

id1=ids.get(c1) if ids.has_key(c1) else currid+1
Run Code Online (Sandbox Code Playgroud)

但是,如果if情况被执行并且如果if条件通过则将c-> id1粘贴到字典中,那么我就会坚持使用currid和sticking.

python dictionary if-statement

6
推荐指数
1
解决办法
248
查看次数

使用Django模型查询时如何确保像NOLOCK这样的SQL

我有一个Django模型"用户"绑定到现有的MS SQL Server数据库表.我正在读这张桌子:

Users.objects.filter(userid='xyz').filter(status='active')
Run Code Online (Sandbox Code Playgroud)

我想知道这会转换成什么样的锁结构,就像在这种读取锁定表一样?在SQL中我会做到:

SELECT * from users (nolock) where userid='xyz' and status='active'
Run Code Online (Sandbox Code Playgroud)

有没有办法通过Django模型查询显式指定"nolock"?

在Django以及django-pyodbc文档中搜索了很多内容但没有任何成功.

谢谢.

ps:使用django-pyodbc和pyodbc驱动程序

sql django pyodbc

5
推荐指数
1
解决办法
770
查看次数

在python列表迭代期间临时创建?

我想了解为什么会发生以下情况.我的猜测是在列表迭代期间正在创建临时,但是需要一些专家来确认:

def test():
    a=[set([1,2,3]),set([3,4,5])]
    x=set([1,4])
    for i in a:
        # doesn't actually modify list contents, making a copy of list elements in i?
        i=i.difference(x)
    print a
    for idx,i in enumerate(a):
        i=i.difference(x)
        print id(i),id(a[idx])
        # obviously this modifies the contents
        a[idx]=i
    print a
Run Code Online (Sandbox Code Playgroud)

输出:

[set([1, 2, 3]), set([3, 4, 5])]
59672976 59672616
59672616 59672736
[set([2, 3]), set([3, 5])]
Run Code Online (Sandbox Code Playgroud)

另外,我想理解为什么第二次迭代中i的"id"与[0]的"id"相同.

python iteration list

5
推荐指数
1
解决办法
1455
查看次数

Backbone.js"胖路由器"设计难题

我花了最近两周的时间学习骨干和相关工具以及编写应用程序.我遇到了设计问题,想知道可用的解决方案类型,以及Backbone专家是否认为这是一个问题.

问题:我最终不得不将所有视图依赖项放在我的router.js中,并且无法弄清楚它们是否是一种方法.以下是我的router.js的代码:

// router.js
define([
  'jquery',
  'underscore',
  'backbone',
  'text',
  'views/landing',
  'views/dashboard',
],  
    function($, _, Backbone, t,LandingView,DashboardView){
        var AppRouter = Backbone.Router.extend({
        routes: {
          // Define some URL routes
          '': 'showLanding',
          'projects': 'showProjects',
          // Default
          '*actions': 'defaultAction'
        },
        navigate_to: function(model){
                alert("navigate_to");
            },

        showProjects: function() {},
        showLanding: function() {},
    });

    var initialize = function() {
        var app_router = new AppRouter;
        Backbone.View.prototype.event_aggregator = _.extend({}, Backbone.Events);
        // Extend the View class to include a navigation method goTo
        Backbone.View.prototype.goTo = function (loc) {
            app_router.navigate(loc, true);
        }; …
Run Code Online (Sandbox Code Playgroud)

javascript design-patterns eventaggregator backbone.js marionette

5
推荐指数
1
解决办法
2334
查看次数

为什么Python Iterator需要一个只返回self的iter方法?

我理解标准说它确实如此,但我试图找到其根本原因.

如果它只是总是返回self它的需要?

您显然总是可以访问该对象,因为您正在调用iter该对象,那么需要它有什么需要?

python iterator

4
推荐指数
1
解决办法
385
查看次数

将mapreduce的Python脚本传递给HBase

我们在Hadoop上实现了HBase.截至目前,我们所有的Map-Reduce作业都是作为Java类编写的.我想知道是否有一种很好的方法可以使用Python脚本传递给HBase进行Map-Reduce.

python hadoop hbase mapreduce

3
推荐指数
1
解决办法
3713
查看次数

Django inheritance base class super

I would like to use inheritance and have all my resources inheritance from a base resource class.

You will see what I have tried so far below. My issues is I now need to add in the meta class at it seems to overwrite at the moment. How can this be done?

class BasedModelResource(ModelResource):
    class Meta:
        authentication = ApiKeyAuthentication()
        authorization = UserObjectsOnlyAuthorization()



class AccountResource(BasedModelResource):
    """
    Account Object Resource
    """
    class Meta:
        queryset = Account.objects.all()
        resource_name = 'account'
Run Code Online (Sandbox Code Playgroud)

django tastypie

3
推荐指数
1
解决办法
680
查看次数