就運行時性能而言,在int中將int轉換爲short的代價是多麼昂貴?可能有成千上萬的這樣的演員,因此我想知道它是否會影響演出。謝謝。在int中使用int轉換爲short的代價是多麼昂貴
回答
您可以忽略該演員的費用。你不會注意到成千上萬的這樣的演員。
我認爲安全的做法是不擔心性能,直到你有性能問題。當你確實遇到性能問題時,很可能在大多數業務應用程序中,大多數應用程序的遲緩可以在與磁盤和/或網絡的交互中被考慮。我認爲這樣的微觀優化不太可能會對你的性能產生很大的影響。
我不同意,它很好地記住性能。由於考慮到性能的開發人員或者因爲同樣原因導致的錯誤,我必須修復太多代碼。 – 2011-05-12 19:46:20
有一些簡單而安全的方法可以保持性能,而不必進入優化領域。例如不在循環內創建臨時對象而不必嚴格需要,避免自動裝箱等等。如果可能的話,優化應該是最後一件事,並且完全避免。代碼的功能性,可維護性和正確性優於最佳和破壞。 – locka 2011-05-12 19:57:01
@John - 除非您對jvm如何在運行時優化您的代碼有非常深入的理解,否則我認爲大多數微觀優化充其量只是猜測。 – DaveH 2011-05-12 20:07:26
爲什麼你需要這樣做?我不認爲這會影響性能多,但要記住的範圍內的數據類型,你需要:與從存儲器加載一個int或存儲短相比
int: -2,147,483,648 to 2,147,483,647
short: -32,768 to 32,767
演員小。無論如何,它們都花費大約2納秒。如果你做了成千上萬的這些,它將花費幾個微秒。
下面是一個長案例的測試,但可以很容易地適應短。 在這個例子中,將整數投射到一個較長的距離比較長,相比慢5%左右。
有趣的是,使用隱式轉換調用該方法比較慢,感謝您自己來鑄造它。
由於沒有投:PT0.141517096S 採用鑄造:PT0.148024511S 隨着隱式轉換:PT0.159904349S
@Test
public void testPerformance(){
long sum =0L;
long timeCallWithImplicitCast =0;
long timeCallWithLong =0;
long timeCallWithCast =0;
for(int j=0;j<2;j++) {//First run warm-up
timeCallWithCast=0;
timeCallWithLong=0;
timeCallWithImplicitCast=0;
for (int i = 0; i < 10_000_000; i++) {
long s1 = System.nanoTime();
sum += shift(i);//Call with int implicit cast
long e1 = System.nanoTime();
timeCallWithImplicitCast += (e1 - s1);
}
for (int i = 0; i < 10_000_000; i++) {
long s3 = System.nanoTime();
sum += shift((long) i);//Call with cast long
long e3 = System.nanoTime();
timeCallWithCast += (e3 - s3);
}
for (int i = 0; i < 10_000_000; i++) {
long l = (long) i;
long s2 = System.nanoTime();
sum += shift(l);//Call with long
long e2 = System.nanoTime();
timeCallWithLong += (e2 - s2);
}
}
System.out.println("With no cast : "+ Duration.ofNanos(timeCallWithLong));
System.out.println("With cast : "+Duration.ofNanos(timeCallWithCast));
System.out.println("With implicit cast: "+Duration.ofNanos(timeCallWithImplicitCast));
}
protected long shift(long index){
return index << 4;
}
最後用某種「證明」來回答。 – bvdb 2015-08-01 13:51:11
離開filosophical論點和藉口擱置...
的所有short
第一是一種特殊的數據類型。 Java並不真的喜歡short
。 Java實際上真的很喜歡int
,因爲JVM堆棧使用32位寄存器。 short
是一個16位數據類型(int
= 32位)。
由於是32位結構,無論何時Java將一個short移動到堆棧,它都會自動轉換爲整數。所以,首先想知道的是,我想在java中使用short
的嗎?他們確實付出了代價。這就是爲什麼你很少會在jdk源代碼中看到任何short
數據類型的用法。
JVM在將整數轉換爲short時使用i2s
操作。 確切的成本將取決於您使用的JVM和硬件。
你可以找到一些數據in this paper,但i2s
不幸被列出。但它應該少於20ns。
- 1. 將short int []轉換爲char *
- 2. 將unsigned short *轉換爲int *
- 3. java將int轉換爲short
- 4. 在java中反射或反省的代價是多麼昂貴
- 5. 是鑄造枚舉int昂貴?
- 6. C++轉換錯誤:從短int *無效轉換爲short int
- 7. 將int轉換爲unsigned short java
- 8. 如何將int []轉換爲short []?
- 9. 爲什麼類型轉換昂貴?
- 10. C#轉換int到short,然後以字節和背部爲int
- 11. 爲什麼在java中將`char`轉換爲`int`而不是`short`或`byte`?
- 12. 在django中放置一箇中間件的代價是多麼昂貴
- 13. 爲什麼不能將.Net從short轉換爲int?
- 14. 爲什麼試塊價格昂貴?
- 15. 不使用int轉換爲Int錯誤
- 16. 使用short int時突然跳轉值
- 17. 在Haskell中將Maybe Int轉換爲Int
- 18. 在'C'中將int轉換爲int []
- 19. 將未知長\ int \ short轉換爲C中的兩倍#
- 20. 使用LINQ轉換爲Int
- 21. 將[Int?]轉換爲[Int]
- 22. 從int *轉換爲int&
- 23. 將int轉換爲int []
- 24. 將IO Int轉換爲Int
- 25. 從int []轉換爲int *
- 26. 轉換int數組爲int
- 27. 枚舉爲int,爲什麼使用類型轉換爲int而不是Convert.ToInt32(Enum)?
- 28. 使用2的補碼將int從int轉換爲int
- 29. d3的selection.text()價格昂貴
- 30. 從const int轉換爲C++中的int
一個快速簡單的基準應該可以解決你的問題。我猜你不應該太擔心表演。 – 2011-05-12 19:36:52
取決於您的申請。 – 2011-05-12 19:39:26