小编Ale*_*ack的帖子

集合的快速交集:C++与C#

在我的机器上(Quad core,8gb ram),运行Vista x64 Business,使用Visual Studio 2008 SP1,我试图非常快地交叉两组数字.

我在C++中实现了两种方法,在C#中实现了一种方法.到目前为止,C#方法更快,我想改进C++方法,因此它比C#更快,我希望C++可以做到.

这是C#输出:(发布版本)

Found the intersection 1000 times, in 4741.407 ms
Run Code Online (Sandbox Code Playgroud)

以下是两种不同方法(Release x64 build)的初始C++输出:

Found the intersection (using unordered_map) 1000 times, in 21580.7ms
Found the intersection (using set_intersection) 1000 times, in 22366.6ms
Run Code Online (Sandbox Code Playgroud)

这是最新的C++输出,有三种方法(Release x64 build):

最新基准:

Found the intersection of 504 values (using unordered_map) 1000 times, in 28827.6ms
Found the intersection of 495 values (using set_intersection) 1000 times, in 9817.69ms
Found the intersection of 504 values (using unordered_set) 1000 times, in 24769.1ms
Run Code Online (Sandbox Code Playgroud)

因此,set_intersection方法现在比C#慢约2倍,但比初始C++方法快2倍. …

c++ performance stl intersection

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

具有类似于shell命令行完成的TAB完成/自动填充的Ajax自动完成(或自动提示)?

我正在实现一个AJAX自动完成/自动提示功能,我不仅要做与用户输入类似的常用显示建议,而且我想让用户进行部分完成以节省输入.

因此,想象一下我的词典中有这些价值:"青苹果","绿色梨","绿色水果","蓝天","蓝色水","蓝色唤醒".

如果用户键入"g",建议应该是"青苹果","绿色梨","绿色水果",我想让用户点击TAB或其他东西将他的查询更新为"绿色",然后他们可以输入"a",他们就会完成"青苹果".

我试图在linux shell命令行完成后对此进行建模.

你能推荐一个这样做的控件/脚本吗?或者对现有控件进行修改/定制?

javascript ajax autocomplete tab-completion

8
推荐指数
1
解决办法
6319
查看次数

使用AndroidStudio运行JUnit测试的异常:java.lang.NoClassDefFoundError:android/os/Parcelable

我根据http://tools.android.com/tech-docs/unit-testing-support创建了单元测试,但是当我运行它们时,我得到一个异常:

java.lang.NoClassDefFoundError: android/os/Parcelable
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClass(ClassLoader.java:800)
    at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
    at java.net.URLClassLoader.defineClass(URLClassLoader.java:449)
    at java.net.URLClassLoader.access$100(URLClassLoader.java:71)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
    at java.lang.Class.getDeclaredMethods0(Native Method)
    at java.lang.Class.privateGetDeclaredMethods(Class.java:2615)
    at java.lang.Class.getDeclaredMethods(Class.java:1860)
    at org.junit.internal.MethodSorter.getDeclaredMethods(MethodSorter.java:54)
    at org.junit.internal.runners.JUnit38ClassRunner.<init>(JUnit38ClassRunner.java:74)
    at org.junit.internal.builders.JUnit3Builder.runnerForClass(JUnit3Builder.java:11)
    at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59)
    at org.junit.internal.builders.AllDefaultPossibilitiesBuilder.runnerForClass(AllDefaultPossibilitiesBuilder.java:26)
    at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59)
    at org.junit.internal.requests.ClassRequest.getRunner(ClassRequest.java:33)
    at org.junit.internal.requests.FilterRequest.getRunner(FilterRequest.java:36)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:41)
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:211)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:67)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:134)
Caused by: java.lang.ClassNotFoundException: android.os.Parcelable
    at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:354) …
Run Code Online (Sandbox Code Playgroud)

java android unit-testing junit4 android-studio

8
推荐指数
1
解决办法
2264
查看次数

快速C++容器,如C#HashSet <T>和Dictionary <K,V>?

我在C#中经常使用HashSet和Dictionary,发现它们非常快......

我已经尝试过使用std :: map和std :: hash_map,并且在比较中发现它们非常慢.这听起来像预期的行为吗?在使用std :: hash_map时,我可能做错了吗?

或者,那里有更好的C++ Hash容器吗?

我正在散列int32s,通常大约有100,000个.

更新:我在C#和C++中创建了一个repro.它进行了两次试验,它们在C#中占用19ms和13ms,在C++中占用大约11,000ms.我的C++代码肯定有问题:)

(两者都作为发布版本运行,两者都是控制台应用程序)

C#输出:

Found 511 values in the intersection, in 19 ms
Found 508 values in the intersection, in 13 ms
Run Code Online (Sandbox Code Playgroud)

C++输出:

Found 308 values in the intersection, in 11764.7ms
Found 316 values in the intersection, in 11742.8ms
Run Code Online (Sandbox Code Playgroud)

C++输出(使用stdext :: hash_map而不是std :: map)

Found 300 values in the intersection, in 383.552ms
Found 306 values in the intersection, in 2277.02ms
Run Code Online (Sandbox Code Playgroud)

C++输出(使用stdext :: hash_map,发布x64版本)

Found 292 values in the intersection, in …
Run Code Online (Sandbox Code Playgroud)

c++ hashtable hashmap

7
推荐指数
1
解决办法
8686
查看次数

在Eclipse-CDT中使用*relative*路径引用第三方库

在Eclipse-CDT中使用相对路径引用第三方C++组件(例如包含库)的最佳实践是什么?

  • 我试图引用Boost和Google protobuf.
  • 我把它们放在我工作区的一个文件夹中,例如/ home/user/workspace/boost_1_39_9
  • 我在项目属性的构建设置下添加了对该文件夹的引用,但是eclipse不允许我将该路径指定为相对于工作区文件夹,我必须使用文件系统选择它并指定绝对路径

谢谢,

  • 亚历克斯

c++ eclipse-cdt

7
推荐指数
2
解决办法
7313
查看次数

Scala:什么是在泛型类中进行数值运算的最佳方法?

在Scala中,我希望能够编写使用>,/,*等运算符的泛型类,但是我没有看到如何约束T这样才能使用.

我考虑使用Ordered [T]约束T,但这似乎不起作用,因为只有RichXXX(例如RichInt)扩展它,而不是Int等.我也看到了Numeric [T],这只在Scala 2.8中可用吗?

这是一个具体的例子:

class MaxOfList[T](list: List[T] ) {
  def max = {
    val seed: Option[T] = None

    list
      .map( t => Some(t))
      // Get the max      
      .foldLeft(seed)((i,m) => getMax(i,m) )
  }

  private def getMax(x: Option[T], y: Option[T]) = {
    if ( x.isDefined && y.isDefined )
      if ( x > y ) x else y
    else if ( x.isDefined )
      x
    else
      y
  }
}
Run Code Online (Sandbox Code Playgroud)

这个类不会编译,因为有很多Ts不支持>等.

思考?

现在我使用了MixIn特性来解决这个问题:

/** Defines a trait that can get the max of …
Run Code Online (Sandbox Code Playgroud)

generics numerical scala

7
推荐指数
1
解决办法
1868
查看次数

boost :: asio:如何使用async_accept接受传入连接?

我正在使用boost :: asio,我有这样的代码:

void CServer::Start(int port)
{ 
    tcp::acceptor acceptor(m_IoService, tcp::endpoint(tcp::v4(), port));

    for ( ;; )
    {
        shared_ptr<tcp::socket> pSocket(new tcp::socket(m_IoService));

        acceptor.accept(*pSocket);

        HandleRequest(pSocket);
    }
}
Run Code Online (Sandbox Code Playgroud)

这段代码有效,但我想切换到使用Acceptor :: async_accept,以便我可以调用Acceptor :: cancel来停止接收请求.

所以我的新代码看起来像这样:

void CServer::StartAsync(int port)
{ 
    m_pAcceptor = shared_ptr<tcp::acceptor>( new tcp::acceptor(m_IoService, tcp::endpoint(tcp::v4(), port)) );

    StartAccept();
}

void CServer::StopAsync()
{
    m_pAcceptor->cancel();
}

void CServer::StartAccept()
{
    shared_ptr<tcp::socket> pSocket(new tcp::socket(m_IoService));

    m_pAcceptor->async_accept(*pSocket, bind(&CServer::HandleAccept, this, pSocket));   
}

void CServer::HandleAccept(shared_ptr<tcp::socket> pSocket)
{
    HandleRequest(pSocket);

    StartAccept();
}
Run Code Online (Sandbox Code Playgroud)

但是这段代码似乎不起作用,我的函数CServer :: HandleAccept永远不会被调用.有任何想法吗?我查看了示例代码,我的代码和他们的代码之间的主要区别是它们似乎经常创建一个类似tcp_connection的类,其中有套接字作为成员,我不明白为什么这是必要的.

  • 亚历克斯

c++ sockets asynchronous boost-asio

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

Scala:像Option(Some,None)之类的东西,但有三种状态:Some,None,Unknown

我需要返回值,当有人要求值时,告诉他们三件事之一:

  1. 这是价值
  2. 没有价值
  3. 我们没有关于此值的信息(未知)

案例2与案例3略有不同.示例:

val radio = car.radioType
Run Code Online (Sandbox Code Playgroud)
  1. 我们知道价值:返回收音机类型,说"先锋"
  2. 湾 没有价值:返回无
  3. C.我们缺少关于这辆车的数据,我们不知道它是否有收音机

我以为我可以扩展scala的None并创建一个Unknown,但这似乎不可能.

建议?

谢谢!

更新:

理想情况下,我希望能够编写如下代码:

car.radioType match { 
   case Unknown => 
   case None => 
   case Some(radioType : RadioType) => 
}
Run Code Online (Sandbox Code Playgroud)

scala nullable option

6
推荐指数
3
解决办法
3260
查看次数

如何让XStream很好地输出Scala列表?我可以写自定义转换器吗?

这段代码:

println(new XStream.toXML(List(1,2,3)))
Run Code Online (Sandbox Code Playgroud)

生成这个XML:

<scala.coloncolon serialization="custom">
  <unserializable-parents/>
  <scala.coloncolon>
    <int>1</int>
    <int>2</int>
    <int>3</int>
    <scala.ListSerializeEnd/>
  </scala.coloncolon>
</scala.coloncolon>
Run Code Online (Sandbox Code Playgroud)

相反,我想这样:

<list>
  <int>1</int>
  <int>2</int>
  <int>3</int>
</list>
Run Code Online (Sandbox Code Playgroud)

这类似于通用java集合的序列化方式.什么是最好的方法呢?

通过实现我自己的转换器,我已经完成了大部分工作,但是我坚持使用unmarshal方法,它不清楚如何实例化一个空列表...

class ListConverter( _mapper : Mapper )  extends AbstractCollectionConverter(_mapper) {
  /** Helper method to use x.getClass
   * 
   * See: http://scalide.blogspot.com/2009/06/getanyclass-tip.html
   */
  def getAnyClass(x: Any) = x.asInstanceOf[AnyRef].getClass

  def canConvert( clazz: Class[_]) = {       
    classOf[::[_]] == clazz
  }

  def marshal( value: Any, writer: HierarchicalStreamWriter, context: MarshallingContext) = {
    val list = value.asInstanceOf[List[_]]
    for ( item <- list ) {      
      writeItem(item, context, …
Run Code Online (Sandbox Code Playgroud)

generics scala list xstream

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

使用jquery时,浏览器能够"记住"表单值吗?

请参阅:http://friendfeed.com/paul/2b7ddce5/jquery-1-3-breaks-bfcache-because-it-binds

我在我的网络应用程序中有一个表单,如果用户提交它并按下浏览器BACK按钮,则会记住表单值(例如用户输入的任何值).

一旦我将jQuery 1.4.2添加到页面(例如将其作为脚本引用),则此行为会发生变化.从我读过的,发生这种情况是因为jQuery挂钩onunload事件,这是一个向浏览器发出的信号,即脚本不支持bfcache,所以它关闭了bfcache.

更新:我进一步研究了这个问题,这个问题在jQuery 1.4中得到了解决.看起来这个问题是由我使用的自动完成插件引起的,如果找到一个,我会发布一个解决方案.

forms jquery back-button

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