在我的机器上(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倍. …
我正在实现一个AJAX自动完成/自动提示功能,我不仅要做与用户输入类似的常用显示建议,而且我想让用户进行部分完成以节省输入.
因此,想象一下我的词典中有这些价值:"青苹果","绿色梨","绿色水果","蓝天","蓝色水","蓝色唤醒".
如果用户键入"g",建议应该是"青苹果","绿色梨","绿色水果",我想让用户点击TAB或其他东西将他的查询更新为"绿色",然后他们可以输入"a",他们就会完成"青苹果".
我试图在linux shell命令行完成后对此进行建模.
你能推荐一个这样做的控件/脚本吗?或者对现有控件进行修改/定制?
我根据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) 我在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) 在Eclipse-CDT中使用相对路径引用第三方C++组件(例如包含库)的最佳实践是什么?
谢谢,
在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) 我正在使用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的类,其中有套接字作为成员,我不明白为什么这是必要的.
我需要返回值,当有人要求值时,告诉他们三件事之一:
案例2与案例3略有不同.示例:
val radio = car.radioType
Run Code Online (Sandbox Code Playgroud)
我以为我可以扩展scala的None并创建一个Unknown,但这似乎不可能.
建议?
谢谢!
更新:
理想情况下,我希望能够编写如下代码:
car.radioType match {
case Unknown =>
case None =>
case Some(radioType : RadioType) =>
}
Run Code Online (Sandbox Code Playgroud) 这段代码:
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) 请参阅: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中得到了解决.看起来这个问题是由我使用的自动完成插件引起的,如果找到一个,我会发布一个解决方案.
c++ ×4
scala ×3
generics ×2
ajax ×1
android ×1
asynchronous ×1
autocomplete ×1
back-button ×1
boost-asio ×1
eclipse-cdt ×1
forms ×1
hashmap ×1
hashtable ×1
intersection ×1
java ×1
javascript ×1
jquery ×1
junit4 ×1
list ×1
nullable ×1
numerical ×1
option ×1
performance ×1
sockets ×1
stl ×1
unit-testing ×1
xstream ×1