这是“更好的”。 AtomicIntegerArray(1/0 as true / false)vs. AtomicBoolean []? [英] Which is "better". AtomicIntegerArray (1/0 as true/false) versus AtomicBoolean[]?

查看:148
本文介绍了这是“更好的”。 AtomicIntegerArray(1/0 as true / false)vs. AtomicBoolean []?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很好奇。如果使用值为0和1的AtomicIntegerArray,您可以完成与AtomicBoolean数组相同的操作。示例:

  final AtomicIntegerArray array1 = new AtomicIntegerArray(10); 
array1.compareAndSet(3,0,1); // 0 = false and 1 = true

//完全一样的东西:

final AtomicBoolean [] array2 = new AtomicBoolean [10];
for(int i = 0; i array2 [i] = new AtomicBoolean(false);
array2 [3] .compareAndSet(false,true);你认为哪一个更快更好?

=h2_lin>解决方案


您认为哪一个更快更好?



$ b b

有趣的问题。如果你做一些非常大的周期,这个速度可能只会是可见的。



在这两种方法下,这两种方法都使用 Unsafe.compareAndSwapInt(...)所以性能可能非常相似。因为没有阻塞访问 volatile 存储,这是不是关于冲突。 AtomicBoolean 数组将确定具有与其相关联的更大数量的对象 - 每个具有它们自己的易失性存储。此外,在 AtomicBoolean 下,将 boolean 值存储为 int 因此没有节省。



我的直觉告诉我使用 AtomicIntegerArray 。更少的代码为你写的通常意味着更多依赖JDK做正确的事情。为了弄清楚,你必须在你的生产架构上测试一些大量的迭代,确定知道。我怀疑差异可以忽略不计,很难衡量。



不是一个很好的答案,但希望在这里有所帮助。



编辑:



所以我只是进行了一些测试,我看不到任何明显的差异。这是我的小测试计划。它使用100个线程,并运行1000万次迭代,他们在彼此的0-10%之内。正如@mttdbrd指出的,这不是一个现实生活测试。



编辑

>

在调整我的程序后,确保我为每个@ mttdbrd的文档加热了热点编译器,并更改程序以能够更好地调整条目数,我看到一些有趣的

  AtomicIntegerArray在数组中有1000个元素:

4224 millis
AtomicBoolean [] in 3546 millis(always a little bit faster)

10个元素:

  AtomicIntegerArray in 26506 millis 
AtomicBoolean [] in 13263 millis $ b

请注意一般的速度差异。这是有道理的,因为有更多的线程争用。 100个线程更有可能旋转10个元素而不是1000个。



这是什么意思?如果你从一个更改到另一个,你可以保存自己每个操作最多1 纳秒。威力。因此,不要担心两者的表现,你应该选择最干净,最容易维护的模式。


I am very curious about that. If you use AtomicIntegerArray with values 0 and 1 you can accomplish the same thing of an AtomicBoolean array. Example:

final AtomicIntegerArray array1 = new AtomicIntegerArray(10);
array1.compareAndSet(3, 0, 1); // 0 = false and 1 = true

// exactly the same thing of:

final AtomicBoolean[] array2 = new AtomicBoolean[10];
for(int i = 0; i < array2.length; i++)
     array2[i] = new AtomicBoolean(false);
array2[3].compareAndSet(false, true);

Which one do you think is faster and better?

解决方案

Which one do you think is faster and better?

Interesting question. The speed of this would probably only be visible if you are doing some very large number of cycles. Otherwise worrying about it smacks as premature optimization.

Under the covers, both methods use the Unsafe.compareAndSwapInt(...) so the performance may be very similar. Since there is no blocking with accessing of volatile storage, this is not about collisions. The AtomicBoolean array will certain have a larger number of objects associated with it -- each with their own volatile storage. Also, under the covers the AtomicBoolean stores the boolean value as an int so no savings there.

My instinct tells me to use the AtomicIntegerArray. Less code for you to write which typically means more reliance on the JDK to do the right thing. To figure it out you would have to test some large number of iterations on your production architecture to know for sure. I suspect the difference is going to be negligible and hard to measure.

Not a great answer but hopefully something helpful here.

Edit:

So I just ran some tests and I can't see any significant differences. Here's my little test program. It used 100 threads and ran 10 million iterations and they were within 0-10% of each other. As @mttdbrd points out, this is in no way a "real life" test. Only benching this in production with the code actually functioning like it should before you truly know if there is a difference.

Edit:

Ok after tweaking my program to make sure I warmed up the hotspot compiler per @mttdbrd's document, and changing the program to be able to better tune the number of entries, I see some interesting results.

With 1000 elements in the arrays:

AtomicIntegerArray in 4224 millis
AtomicBoolean[]    in 3546 millis    (always a little bit faster)

However with 10 elements in the array:

AtomicIntegerArray in 26506 millis
AtomicBoolean[]    in 13263 millis  (much faster)

Notice also the speed difference in general. It makes sense since there is more thread contention. 100 threads are much more likely to have to spin with 10 elements instead of 1000.

What does this mean? That if you change from one to the other you might save yourself at most 1 nanosecond per operation. Might. So instead of worrying about the performance of the two, you should pick the pattern that is the cleanest and most easily maintained.

这篇关于这是“更好的”。 AtomicIntegerArray(1/0 as true / false)vs. AtomicBoolean []?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆