Android DualCore Handy LG Optimus 与 Java 线程的速度没有提升?

发布于 2024-11-05 00:59:29 字数 776 浏览 5 评论 0原文

我正在研究 LG P990 optimus 的速度,发现使用多线程根本无法获得任何加速。

我使用以下代码来测量某些计算所需的时间。

public class TestThreads extends Thread{

public void run()
{
double temp;
    for(int i = 0; i < 5000000 ;i++)
    {
        temp = Math.random()*Math.random();
    }
}

}

    long start = System.currentTimeMillis();
    Thread t1 = new TestThreads();
    Thread t2 = new TestThreads();
    t1.start();
    t2.start();
t1.join();
t2.join();

我将生成的时间与计算所需的时间进行了比较,

    for(int i = 0; i < 10000000 ;i++)
    {
        temp = Math.random()*Math.random();
    }

因为 2 线程版本计算相同数量的循环,但分布在可能并行运行的 2 个线程上,所以我预计该版本会明显更快。然而,根本没有加速,在某些情况下,线程版本甚至更慢。我的想法/代码有问题吗?还是 Android 没有在多个 CPU 核心上分配多个线程?

I was playing around with the LG LG P990 optimus speed and noticed that I could not get any speedup at all using multiple threads.

I used the following code for to measure the time needed for some computations.

public class TestThreads extends Thread{

public void run()
{
double temp;
    for(int i = 0; i < 5000000 ;i++)
    {
        temp = Math.random()*Math.random();
    }
}

}

    long start = System.currentTimeMillis();
    Thread t1 = new TestThreads();
    Thread t2 = new TestThreads();
    t1.start();
    t2.start();
t1.join();
t2.join();

The resulting time I compared to the the needed to calculate

    for(int i = 0; i < 10000000 ;i++)
    {
        temp = Math.random()*Math.random();
    }

Since the 2 Threaded Version calculates the same amount of loops but distributed over 2 Threads which could possibly run parallel I expected this Version to be significantly faster. However there was no speedup at all and in some cases the Threaded Version was even slower. is there a problem with my idea / code or does Android not distribute multiple threads accross multiple CPU cores?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

喜爱纠缠 2024-11-12 00:59:29

Math.random() 不是线程安全的,它有一个控制访问的互斥锁,这会增加开销并减慢线程速度。您可以在这里阅读http:// /code-o-matic.blogspot.com/2009/01/beware-of-hidden-contention-of.html

尝试做其他线程安全的事情。

Math.random() is not thread safe, it has a mutex that controls access, which adds overhead and slows the threads down. You can read about it here http://code-o-matic.blogspot.com/2009/01/beware-of-hidden-contention-of.html

Try doing something else that is thread safe.

原野 2024-11-12 00:59:29

首先,每当您使用线程时,您都会增加内存开销,因为您创建了额外的对象,并且必须在 java 虚拟机中为它们分配内存。

其次,即使在具有单个线程的应用程序中,您也可以使用和利用 2 个 CPU。这是事实,因为即使应用程序在单个处理器中执行,另一个处理器也会处理来自其他应用程序的其他任务,从而使第一个处理器在更多时间内处于空闲状态,并允许更连续地处理应用程序。

无论如何,操作系统决定在给定时间处理哪个应用程序以及在哪个处理器中处理它。

但由于这些原因,在某些情况下,即使使用其他线程安全的方法,特别是当设备中有很多数据需要处理时,开销也可能不值得。然而,使用线程安全方法(与 Math.random() 不同),在最坏的情况下应该会有所收获,即使很小。

First of all, whenever you use Threads you are increasing the amount of memory overhead, because of the additional objects you create and the memory that must be assigned for them within the java virtual machine.

Second, the fact you have 2 CPUs can be used and taken advantage of even in an application with a single Thread. This is true because even with the application being executed in a single processor, the other will be processing other tasks from other applications, consequently leaving the first one free during more time, and allowing a more continuous processing of your application.

In any case, it is the operating system that decides which application is processed at a given time, and in which processor it is processed.

But for those reasons, it is possible that in some occasions the overhead might not be worth the gain, even when using other methods which are thread safe, specially when there is much to be processed in the device. However, with a threadsafe method (unlike Math.random()), there should be gains, even if small, in a worst-case situation.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文