我创建了这个小程序,用概率和比率来计算pi.为了让它运行得更快我决定用pthreads多线程一次.不幸的是,即使经过大量的搜索,我也无法解决我的问题,当我运行threadFunc函数时,使用一个线程,无论是使用pthread,还是通常从calculate_pi_mt函数调用,性能都很高比我在双核机器上使用两个线程运行它时更好(至少两次或者不是三倍).我试过禁用优化无济于事.据我所知,当线程运行时,它使用局部变量,而不是在最后我使用互斥锁来创建命中总和...
首先是否有任何创建代码的技巧可以在这里运行得更好?(即风格),因为我只是通过尝试这些东西来学习.
其次,这些明显的性能问题是否有任何原因?当线程数设置为1运行时,我的一个cpus最大值为100%.设置为2时,第二个cpu上升到大约80%-90%,但显然这样做的所有额外工作都无济于事!可能是使用rand()函数?
struct arguments {
int n_threads;
int rays;
int hits_in;
pthread_mutex_t *mutex;
};
void *threadFunc(void *arg)
{
struct arguments* args=(struct arguments*)arg;
int n = 0;
int local_hits_in = 0;
double x;
double y;
double r;
while (n < args->rays)
{
n++;
x = ((double)rand())/((double)RAND_MAX);
y = ((double)rand())/((double)RAND_MAX);
r = (double)sqrt(pow(x, 2) + pow(y, 2));
if (r < 1.0){
local_hits_in++;
}
}
pthread_mutex_lock(args->mutex);
args->hits_in += local_hits_in;
pthread_mutex_unlock(args->mutex);
return NULL;
}
double calculate_pi_mt(int rays, int threads){
double answer;
int c; …Run Code Online (Sandbox Code Playgroud) I have Database (compact sql server) in a*.sdf file. I converted a compact framework project to a regular pc project and for some reason, with the same code, and the same*.sdf file the following exception was thrown: "the file name is not valid. check the file name for the database"
the weird thing is that on my Pc - the substring "file:\" was added to my original connection string and that's probably why the exception is thrown. when I remove …
我试图用setTimeout()它来暂停JS中的一系列事件.
以下是我正在做的事情以及我想在评论中做些什么的例子 - http://jsfiddle.net/8m5Ww/2/
有什么建议我可以填充var timeRemaining剩余的总毫秒var a?
我有一个关于java SocketChannel的问题.
假设我在阻塞模式下打开了套接字通道; 在调用write(ByteBuffer)方法之后,我得到一个描述写入了多少字节的整数.javadoc说: "返回:写入的字节数,可能为零"
但究竟是什么意思呢?这是否意味着真正传递给客户端的字节数(以便发送方收到tcp ack表明服务器已接收到多少字节),或者这是否意味着已将字节数写入tcp堆栈?(这样一些字节仍然可能正在等待,例如在网卡缓冲区中).
我试图编写一个规范的例子,即'it'应该......"do"是在运行时确定的.我尝试将'it'方法放在我自己的方法中,以便我可以多次调用它:
def new_method(test)
it "#{test} should... " do
end
end
Run Code Online (Sandbox Code Playgroud)
但是,'it'方法不能从当前的Spec :: Example :: ExampleGroup :: Subclass实例中获得.
有人知道如何@@Identity在使用T-Sql时返回吗?
像这样的东西:
set @Sql = "insert into table....values()..."
exec @sql
return @@Identity
Run Code Online (Sandbox Code Playgroud) 无法弄清楚,如何将十进制字段更新为null或空白.
试过:
UPDATE ads SET price=NULL WHERE price=0
Run Code Online (Sandbox Code Playgroud)
和
UPDATE ads SET price="" WHERE price=0
Run Code Online (Sandbox Code Playgroud)
不工作.
谢谢 ;)
(更新)片刻.
我目前正在通过Eclipse编写GWT应用程序.Eclipse用于开发,但我使用m2eclipse插件和带有GWT插件的Maven pom.xml来构建和运行它.
当我需要调试应用程序时,我必须:
我已经将两个目标添加到收藏夹中以将它们锁定在下拉列表中,但它仍然很痛苦.
我想通过单击而不是两个离散的动作按顺序启动它们.
Eclipse中有没有办法实现这一目标?
我在表上使用触发器使用sp_send_dbmail发送电子邮件.
我想在图像类型的电子邮件中包含文件附件.
jpeg的原始数据存储在ndl_Image列中,该列的类型为binary.
我有以下代码: -
DECLARE @ReferenceID varchar(max)
DECLARE @Recipient varchar(Max)
DECLARE @Body varchar(max)
DECLARE @Subject varchar(max)
DECLARE @Q varchar(max)
--Get the EntryId and FormID for the inserted data.
SET @ReferenceID = 40
SET @Recipient = (SELECT ndl_CategorySendTo FROM ndl_config WHERE ndl_CategoryName = 'Dead Animal')
SET @Body = '<html>A new request has been created.</html>'
SET @Subject = 'NDL Report It: New Request #'+@ReferenceID
SET @Q = 'SELECT ndl_Image from dbo.ndl_data where ndl_ID ='+@ReferenceID
--Execute the stored procedure to send mail. …Run Code Online (Sandbox Code Playgroud) 我有类似的字符串
var str = 'One & two & three';
Run Code Online (Sandbox Code Playgroud)
由Web服务器呈现为HTML.我需要将这些字符串转换为
'One & two & three'
Run Code Online (Sandbox Code Playgroud)
目前,这就是我正在做的事情(借助jQuery):
$(document.createElement('div')).html('{{ driver.person.name }}').text()
Run Code Online (Sandbox Code Playgroud)
但是我有一种不安的感觉,我做错了.我试过了
unescape("&")
Run Code Online (Sandbox Code Playgroud)
但它似乎不起作用,decodeURI/decodeURIComponent也没有.
有没有其他更原生和更优雅的方式呢?