小编Gab*_*iMe的帖子

BOOST:递归shared_mutex?

似乎Boost的shared_mutex不是递归的..反正这有什么问题吗?(没有重新实现整个东西)

c++ boost-thread

15
推荐指数
2
解决办法
6860
查看次数

PostgreSQL:如何结合按日期排序的3个表

我有来自3个不同的不相关表的3个不同的SQL查询(全部使用LIMIT和ORDER BY).

我想根据"日期"字段(显示在所有字段中)合并和排序结果

执行此操作的SQL是什么?

postgresql

12
推荐指数
2
解决办法
2万
查看次数

Clojure:如何获取函数的元数据?

我正在尝试获取所有内置Clojure函数的元数据.

之前的问题中,我已经了解到这可以使用类似的东西来实现^#'func_name(获取var对象的元数据).但我没有设法以编程方式进行,其中func-name事先不知道.

例如,尝试获取clojure.core中最后一个函数的元数据:

user=> (use 'clojure.contrib.ns-utils)
nil
user=> (def last-func (last (vars clojure.core)))

user=> last-func
zipmap

;The real metadata (zipmap is hardcoded)
user=> ^#'zipmap
{:ns #<Namespace clojure.core>, :name zipmap, :file "clojure/core.clj", :line 1661, :arglists ([keys vals]), :doc "Returns a map .."}

;Try to get programmatically, but get shit
user=> ^#'last-func
{:ns #<Namespace user>, :name last-func, :file "NO_SOURCE_PATH", :line 282}
Run Code Online (Sandbox Code Playgroud)

怎么做到呢?我已经尝试了很多变化,但没有任何诀窍.

clojure

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

Clojure:懒惰的魔法

几乎有2个相同的程序可以生成无限懒惰的random序列.第一个不会崩溃.第二次崩溃与OutOfMemoryError异常.为什么?

;Return infinite lazy sequence of random numbers    
(defn inf-rand[] (lazy-seq (cons (rand) (inf-rand))))    

;Never returns. Burns the CPU but won't crash and lives forever.    
(last (inf-rand))
Run Code Online (Sandbox Code Playgroud)

但是下面的崩溃很快:

;Return infinite lazy sequence of random numbers    
(defn inf-rand[] (lazy-seq (cons (rand) (inf-rand))))    
(def r1 (inf-rand))

;Crash with "OutOfMemoryError"
 (last r1)
Run Code Online (Sandbox Code Playgroud)

clojure lazy-evaluation lazy-sequences

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

jQuery:如何在提交之前修改表单字段

但没有丑陋的效果,新值显示在表单中.

如果我做:

$('#submit_button').click(function(){
   $('#field1').val('newval'); 
   $('#form1').submit(); });
Run Code Online (Sandbox Code Playgroud)

#field1将显示(一瞬间)新值,这是一种丑陋的..

jquery

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

正则表达速度:在VS2013下,Python比C++ 11快6倍?

可能是python的C正则表达式实现快了6倍还是我错过了什么?

Python版本:

import re
r=re.compile(r'(HELLO).+?(\d+)', re.I)
s=r"prefixdfadfadf adf adf adf adf he asdf dHello Regex 123"

%timeit r.search(s)

1000000 loops, best of 3: 1.3 µs per loop (769,000 per sec)
Run Code Online (Sandbox Code Playgroud)

C++ 11版本:

#include<regex>
int main(int argc, char * argv[])
{
    std::string s = "prefixdfadfadf adf adf adf adf he asdf dHello Regex 123";
    std::regex my(R"((HELLO).+?(\d+))", regex_constants::icase);

    bench_utils::run(std::chrono::seconds(10),
        [&]{
        std::smatch match;
        bool found = std::regex_search(s, match, my);
    });       
    return 0;
}

Results in about ~125,000 searches/second
Run Code Online (Sandbox Code Playgroud)

编辑: 这是bench_utils的代码:

namespace bench_utils
{ …
Run Code Online (Sandbox Code Playgroud)

python regex performance c++11 visual-studio-2013

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

使用一维向量实现非二叉树的算法?

树的每个节点可能具有任意数量的子节点.我需要一种方法来构造和遍历这些树,但是使用一维向量或列表来实现它们.

algorithm

9
推荐指数
1
解决办法
3829
查看次数

Python:多次创建一个小列表有多贵?

我在Python中一遍又一遍地遇到以下小烦恼的困境:

选项1:

如果多次调用,因为为每次调用do_something()重新创建a_list,所以更干净但更慢(?)

def do_something():    
  a_list = ["any", "think", "whatever"]    
  # read something from a_list
Run Code Online (Sandbox Code Playgroud)

选项2:

更丑但更有效率(重新创建a_list创建)

a_list = ["any", "think", "whatever"]    
def do_something():    
  # read something from a_list
Run Code Online (Sandbox Code Playgroud)

你怎么看?

python

9
推荐指数
1
解决办法
427
查看次数

Erlang:使用gen_tcp避免竞争条件:controlling_process

我正在使用以下顺序实现简单的tcp服务器:

{ok, LS} = gen_tcp:listen(Port,[{active, true}, {reuseaddr, true}, {mode, list}]),
{ok, Socket} =  gen_tcp:accept(LS),
Pid = spawn_link(M, F, [Socket]),           
gen_tcp:controlling_process(Socket, Pid) 
Run Code Online (Sandbox Code Playgroud)

使用选项{active,true}可能会导致竞争条件,即在调用"controlling_process"之前新数据包到达套接字进程,这将导致{tcp,Socket,Data}消息到达父进程而不是儿童.

如何避免这种情况?

erlang

9
推荐指数
1
解决办法
1159
查看次数

VC++:性能下降x20当线程多于cpus但不在g ++下

简单的多线程c ++ 11程序,其中所有线程在紧密循环中锁定相同的互斥锁.

当它使用8个线程(作为逻辑cpus的数量)时,它可以达到500万次/秒

但只添加一个额外的线程 - 性能下降到200,000 /秒!

编辑:

在g ++ 4.8.2(ubuntu x64)下:即使有100个线程也没有性能下降!(并且是性能的两倍以上,但这是另一个故事) - 所以这确实是VC++互斥实现的特定问题

我使用以下代码(Windows 7 x64)复制它:

#include <chrono>
#include <thread>
#include <memory>
#include <mutex>
#include <atomic>
#include <sstream>
#include <iostream>

using namespace std::chrono;

void thread_loop(std::mutex* mutex, std::atomic_uint64_t* counter)
{
    while (true)
    {
        std::unique_lock<std::mutex> ul(*mutex);        
        counter->operator++();                    
    }        
}

int _tmain(int argc, _TCHAR* argv[])
{    

    int threads = 9;
    std::mutex mutex;
    std::atomic_uint64_t counter = 0;

    std::cout << "Starting " << threads << " threads.." << std::endl;
    for (int i …
Run Code Online (Sandbox Code Playgroud)

c++ multithreading visual-c++ c++11 visual-studio-2013

9
推荐指数
2
解决办法
743
查看次数