-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi_top_questions.json
More file actions
265 lines (265 loc) · 18.5 KB
/
Copy pathapi_top_questions.json
File metadata and controls
265 lines (265 loc) · 18.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
{
"items" : [ {
"tags" : [ "java", "c++", "performance", "cpu-architecture", "branch-prediction" ],
"owner" : {
"account_id" : 31692,
"reputation" : 506165,
"user_id" : 87234,
"user_type" : "registered",
"accept_rate" : 100,
"profile_image" : "https://i.sstatic.net/FkjBe.png?s=256",
"display_name" : "GManNickG",
"link" : "https://stackoverflow.com/users/87234/gmannickg"
},
"is_answered" : true,
"view_count" : 1959341,
"protected_date" : 1399067470,
"accepted_answer_id" : 11227902,
"answer_count" : 25,
"score" : 27500,
"last_activity_date" : 1753188482,
"creation_date" : 1340805096,
"last_edit_date" : 1748010184,
"question_id" : 11227809,
"content_license" : "CC BY-SA 4.0",
"link" : "https://stackoverflow.com/questions/11227809/why-is-processing-a-sorted-array-faster-than-processing-an-unsorted-array",
"title" : "Why is processing a sorted array faster than processing an unsorted array?",
"body" : "<p>In this C++ code, sorting the data (<em>before</em> the timed region) makes the primary loop ~6x faster:</p>\n<pre class=\"lang-cpp prettyprint-override\"><code>#include <algorithm>\n#include <ctime>\n#include <iostream>\n\nint main()\n{\n // Generate data\n const unsigned arraySize = 32768;\n int data[arraySize];\n\n for (unsigned c = 0; c < arraySize; ++c)\n data[c] = std::rand() % 256;\n\n // !!! With this, the next loop runs faster.\n std::sort(data, data + arraySize);\n\n // Test\n clock_t start = clock();\n long long sum = 0;\n for (unsigned i = 0; i < 100000; ++i)\n {\n for (unsigned c = 0; c < arraySize; ++c)\n { // Primary loop.\n if (data[c] >= 128)\n sum += data[c];\n }\n }\n\n double elapsedTime = static_cast<double>(clock()-start) / CLOCKS_PER_SEC;\n\n std::cout << elapsedTime << '\\n';\n std::cout << "sum = " << sum << '\\n';\n}\n</code></pre>\n<ul>\n<li>Without <code>std::sort(data, data + arraySize);</code>, the code runs in 11.54 seconds.</li>\n<li>With the sorted data, the code runs in 1.93 seconds.</li>\n</ul>\n<p>(Sorting itself takes more time than this one pass over the array, so it's not actually worth doing if we needed to calculate this for an unknown array.)</p>\n<hr />\n<p>Initially, I thought this might be just a language or compiler anomaly, so I tried Java:</p>\n<pre class=\"lang-java prettyprint-override\"><code>import java.util.Arrays;\nimport java.util.Random;\n\npublic class Main\n{\n public static void main(String[] args)\n {\n // Generate data\n int arraySize = 32768;\n int data[] = new int[arraySize];\n\n Random rnd = new Random(0);\n for (int c = 0; c < arraySize; ++c)\n data[c] = rnd.nextInt() % 256;\n\n // !!! With this, the next loop runs faster\n Arrays.sort(data);\n\n // Test\n long start = System.nanoTime();\n long sum = 0;\n for (int i = 0; i < 100000; ++i)\n {\n for (int c = 0; c < arraySize; ++c)\n { // Primary loop.\n if (data[c] >= 128)\n sum += data[c];\n }\n }\n\n System.out.println((System.nanoTime() - start) / 1000000000.0);\n System.out.println("sum = " + sum);\n }\n}\n</code></pre>\n<p>With a similar but less extreme result.</p>\n<hr />\n<p>My first thought was that sorting brings the data into the <a href=\"https://en.wikipedia.org/wiki/CPU_cache\" rel=\"noreferrer\">cache</a>, but that's silly because the array was just generated.</p>\n<ul>\n<li>What is going on?</li>\n<li>Why is processing a sorted array faster than processing an unsorted array?</li>\n</ul>\n<p>The code is summing up some independent terms, so the order should not matter.</p>\n<hr />\n<h2>Related / follow-up Q&As with more modern C++ compilers</h2>\n<ul>\n<li><a href=\"https://stackoverflow.com/q/66521344\">Why is processing an unsorted array the same speed as processing a sorted array with modern x86-64 clang?</a> - <strong>modern C++ compilers auto-vectorize the loop</strong>, especially when SSE4.1 or AVX2 is available. This avoids any data-dependent branching so performance isn't data-dependent.</li>\n<li><a href=\"https://stackoverflow.com/q/28875325\">gcc optimization flag -O3 makes code slower than -O2</a> - branchless scalar with <code>cmov</code> can result in a longer dependency chain (especially when GCC chooses poorly), creating a latency bottleneck that makes it slower than branchy asm for the sorted case.</li>\n</ul>\n"
}, {
"tags" : [ "java", "methods", "parameter-passing", "pass-by-reference", "pass-by-value" ],
"owner" : {
"account_id" : 3050,
"reputation" : 4795,
"user_id" : 4315,
"user_type" : "registered",
"profile_image" : "https://www.gravatar.com/avatar/?s=256&d=identicon&r=PG",
"display_name" : "user4315",
"link" : "https://stackoverflow.com/users/4315/user4315"
},
"is_answered" : true,
"view_count" : 2825121,
"protected_date" : 1308938923,
"answer_count" : 87,
"community_owned_date" : 1347646975,
"score" : 7743,
"locked_date" : 1701244089,
"last_activity_date" : 1754960500,
"creation_date" : 1220386469,
"last_edit_date" : 1676406674,
"question_id" : 40480,
"content_license" : "CC BY-SA 4.0",
"link" : "https://stackoverflow.com/questions/40480/is-java-pass-by-reference-or-pass-by-value",
"title" : "Is Java "pass-by-reference" or "pass-by-value"?",
"body" : "<p>I always thought Java uses <strong>pass-by-reference</strong>. However, I read <a href=\"http://javadude.com/articles/passbyvalue.htm\" rel=\"noreferrer\">a blog post</a> which claims that Java uses <strong>pass-by-value</strong>. I don't think I understand the distinction the author is making.</p>\n<p>What is the explanation?</p>\n"
}, {
"tags" : [ "java", "date", "timezone" ],
"owner" : {
"account_id" : 137777,
"reputation" : 199150,
"user_id" : 342235,
"user_type" : "registered",
"accept_rate" : 77,
"profile_image" : "https://www.gravatar.com/avatar/88e5a9eb011a73ab970ddc8cfb05ff85?s=256&d=identicon&r=PG",
"display_name" : "Freewind",
"link" : "https://stackoverflow.com/users/342235/freewind"
},
"is_answered" : true,
"view_count" : 820291,
"protected_date" : 1433866025,
"accepted_answer_id" : 6841479,
"answer_count" : 11,
"score" : 7613,
"last_activity_date" : 1758885411,
"creation_date" : 1311754558,
"last_edit_date" : 1684606299,
"question_id" : 6841333,
"content_license" : "CC BY-SA 4.0",
"link" : "https://stackoverflow.com/questions/6841333/why-is-subtracting-these-two-epoch-milli-times-in-year-1927-giving-a-strange-r",
"title" : "Why is subtracting these two epoch-milli Times (in year 1927) giving a strange result?",
"body" : "<p>If I run the following program, which parses two date strings referencing times 1 second apart and compares them:</p>\n<pre><code>public static void main(String[] args) throws ParseException {\n SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); \n String str3 = "1927-12-31 23:54:07"; \n String str4 = "1927-12-31 23:54:08"; \n Date sDt3 = sf.parse(str3); \n Date sDt4 = sf.parse(str4); \n long ld3 = sDt3.getTime() /1000; \n long ld4 = sDt4.getTime() /1000;\n System.out.println(ld4-ld3);\n}\n</code></pre>\n<p><strong>The output is:</strong></p>\n<pre><code>353\n</code></pre>\n<p>Why is <code>ld4-ld3</code>, not <code>1</code> (as I would expect from the one-second difference in the times), but <code>353</code>?</p>\n<p>If I change the dates to times 1 second later:</p>\n<pre><code>String str3 = "1927-12-31 23:54:08"; \nString str4 = "1927-12-31 23:54:09"; \n</code></pre>\n<p>Then <code>ld4-ld3</code> will be <code>1</code>.</p>\n<hr />\n<p><strong>Java version:</strong></p>\n<pre class=\"lang-none prettyprint-override\"><code>java version "1.6.0_22"\nJava(TM) SE Runtime Environment (build 1.6.0_22-b04)\nDynamic Code Evolution Client VM (build 0.2-b02-internal, 19.0-b04-internal, mixed mode)\n</code></pre>\n<pre class=\"lang-java prettyprint-override\"><code>Timezone(`TimeZone.getDefault()`):\n\nsun.util.calendar.ZoneInfo[id="Asia/Shanghai",\noffset=28800000,dstSavings=0,\nuseDaylight=false,\ntransitions=19,\nlastRule=null]\n\nLocale(Locale.getDefault()): zh_CN\n</code></pre>\n"
}, {
"tags" : [ "java", "string", "io", "stream", "inputstream" ],
"owner" : {
"account_id" : 9076,
"reputation" : 48581,
"user_id" : 16616,
"user_type" : "registered",
"accept_rate" : 100,
"profile_image" : "https://i.sstatic.net/PNeie.png?s=256",
"display_name" : "Johnny Maelstrom",
"link" : "https://stackoverflow.com/users/16616/johnny-maelstrom"
},
"is_answered" : true,
"view_count" : 2769236,
"protected_date" : 1370840953,
"accepted_answer_id" : 309448,
"answer_count" : 67,
"score" : 4772,
"last_activity_date" : 1734679345,
"creation_date" : 1227286060,
"last_edit_date" : 1663944578,
"question_id" : 309424,
"content_license" : "CC BY-SA 4.0",
"link" : "https://stackoverflow.com/questions/309424/how-do-i-read-convert-an-inputstream-into-a-string-in-java",
"title" : "How do I read / convert an InputStream into a String in Java?",
"body" : "<p>If you have a <code>java.io.InputStream</code> object, how should you process that object and produce a <code>String</code>?</p>\n<hr />\n<p>Suppose I have an <code>InputStream</code> that contains text data, and I want to convert it to a <code>String</code>, so for example I can write that to a log file.</p>\n<p>What is the easiest way to take the <code>InputStream</code> and convert it to a <code>String</code>?</p>\n<pre><code>public String convertStreamToString(InputStream is) {\n// ???\n}\n</code></pre>\n"
}, {
"tags" : [ "java", "nullpointerexception", "null" ],
"owner" : {
"account_id" : 16138,
"reputation" : 3837,
"user_id" : 34856,
"user_type" : "registered",
"profile_image" : "https://www.gravatar.com/avatar/1e57ced4476e0c629c70e6058159c4f0?s=256&d=identicon&r=PG",
"display_name" : "Goran Martinic",
"link" : "https://stackoverflow.com/users/34856/goran-martinic"
},
"is_answered" : true,
"view_count" : 1455426,
"protected_date" : 1368308875,
"accepted_answer_id" : 271874,
"answer_count" : 73,
"community_owned_date" : 1319187757,
"score" : 4439,
"last_activity_date" : 1748456350,
"creation_date" : 1226046700,
"last_edit_date" : 1684928227,
"question_id" : 271526,
"content_license" : "CC BY-SA 4.0",
"link" : "https://stackoverflow.com/questions/271526/how-do-i-avoid-checking-for-nulls-in-java",
"title" : "How do I avoid checking for nulls in Java?",
"body" : "<p>I use <code>x != null</code> to avoid <a href=\"https://docs.oracle.com/javase/9/docs/api/java/lang/NullPointerException.html\" rel=\"noreferrer\"><code>NullPointerException</code></a>. Is there an alternative?</p>\n<pre class=\"lang-java prettyprint-override\"><code>if (x != null) {\n // ...\n}\n</code></pre>\n"
}, {
"tags" : [ "java", "collections", "hashmap", "hashtable" ],
"owner" : {
"account_id" : 3051,
"reputation" : 52253,
"user_id" : 4316,
"user_type" : "registered",
"accept_rate" : 100,
"profile_image" : "https://www.gravatar.com/avatar/dc91d4e8c035283347ba673168770cea?s=256&d=identicon&r=PG",
"display_name" : "dmanxiii",
"link" : "https://stackoverflow.com/users/4316/dmanxiii"
},
"is_answered" : true,
"view_count" : 1781550,
"protected_date" : 1331925209,
"accepted_answer_id" : 40878,
"answer_count" : 35,
"score" : 4347,
"last_activity_date" : 1723484314,
"creation_date" : 1220386320,
"last_edit_date" : 1596280763,
"question_id" : 40471,
"content_license" : "CC BY-SA 4.0",
"link" : "https://stackoverflow.com/questions/40471/what-are-the-differences-between-a-hashmap-and-a-hashtable-in-java",
"title" : "What are the differences between a HashMap and a Hashtable in Java?",
"body" : "<p>What are the differences between a <a href=\"https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/HashMap.html\" rel=\"noreferrer\"><code>HashMap</code></a> and a <a href=\"https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/Hashtable.html\" rel=\"noreferrer\"><code>Hashtable</code></a> in Java?</p>\n\n<p>Which is more efficient for non-threaded applications?</p>\n"
}, {
"tags" : [ "java", "random", "integer" ],
"owner" : {
"account_id" : 18419,
"reputation" : 49733,
"user_id" : 42155,
"user_type" : "registered",
"accept_rate" : 22,
"profile_image" : "https://www.gravatar.com/avatar/bec1c77805ad9d7e6b1dca098442e984?s=256&d=identicon&r=PG",
"display_name" : "user42155",
"link" : "https://stackoverflow.com/users/42155/user42155"
},
"is_answered" : true,
"view_count" : 5336915,
"protected_date" : 1296764202,
"answer_count" : 59,
"score" : 4130,
"last_activity_date" : 1724356383,
"creation_date" : 1229106057,
"last_edit_date" : 1669989965,
"question_id" : 363681,
"content_license" : "CC BY-SA 4.0",
"link" : "https://stackoverflow.com/questions/363681/how-do-i-generate-random-integers-within-a-specific-range-in-java",
"title" : "How do I generate random integers within a specific range in Java?",
"body" : "<p>How do I generate a random <code>int</code> value in a specific range?</p>\n<p>The following methods have bugs related to integer overflow:</p>\n<pre class=\"lang-java prettyprint-override\"><code>randomNum = minimum + (int)(Math.random() * maximum);\n// Bug: `randomNum` can be bigger than `maximum`.\n</code></pre>\n<pre class=\"lang-java prettyprint-override\"><code>Random rn = new Random();\nint n = maximum - minimum + 1;\nint i = rn.nextInt() % n;\nrandomNum = minimum + i;\n// Bug: `randomNum` can be smaller than `minimum`.\n</code></pre>\n"
}, {
"tags" : [ "java", "arrays", "arraylist", "type-conversion" ],
"owner" : {
"account_id" : 717,
"reputation" : 54778,
"user_id" : 939,
"user_type" : "registered",
"accept_rate" : 81,
"profile_image" : "https://www.gravatar.com/avatar/ce6d3fed32e985093a107913c3762579?s=256&d=identicon&r=PG",
"display_name" : "Ron Tuffin",
"link" : "https://stackoverflow.com/users/939/ron-tuffin"
},
"is_answered" : true,
"view_count" : 1895080,
"protected_date" : 1479928258,
"accepted_answer_id" : 157950,
"answer_count" : 42,
"score" : 4129,
"last_activity_date" : 1700492459,
"creation_date" : 1222871912,
"last_edit_date" : 1696837864,
"question_id" : 157944,
"content_license" : "CC BY-SA 4.0",
"link" : "https://stackoverflow.com/questions/157944/create-arraylist-from-array",
"title" : "Create ArrayList from array",
"body" : "<pre class=\"lang-java prettyprint-override\"><code>Element[] array = {new Element(1), new Element(2), new Element(3)};\n</code></pre>\n<p>How do I convert the above variable of type <code>Element[]</code> into a variable of type <a href=\"https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/ArrayList.html\" rel=\"noreferrer\"><code>ArrayList<Element></code></a>?</p>\n<pre class=\"lang-java prettyprint-override\"><code>ArrayList<Element> arrayList = ...;\n</code></pre>\n"
}, {
"tags" : [ "java", "android", "usermanager" ],
"owner" : {
"account_id" : 259304,
"reputation" : 72401,
"user_id" : 542091,
"user_type" : "registered",
"accept_rate" : 92,
"profile_image" : "https://i.sstatic.net/Y985l.jpg?s=256",
"display_name" : "Ovidiu Latcu",
"link" : "https://stackoverflow.com/users/542091/ovidiu-latcu"
},
"is_answered" : true,
"view_count" : 377654,
"protected_date" : 1353543106,
"accepted_answer_id" : 13375461,
"answer_count" : 12,
"score" : 4055,
"last_activity_date" : 1725273063,
"creation_date" : 1352882041,
"last_edit_date" : 1536308514,
"question_id" : 13375357,
"content_license" : "CC BY-SA 4.0",
"link" : "https://stackoverflow.com/questions/13375357/proper-use-cases-for-android-usermanager-isuseragoat",
"title" : "Proper use cases for Android UserManager.isUserAGoat()?",
"body" : "<p>I was looking at the new APIs introduced in <a href=\"http://en.wikipedia.org/wiki/Android_version_history#Android_4.1.2F4.2_Jelly_Bean\" rel=\"noreferrer\">Android 4.2</a>.\nWhile looking at the <a href=\"http://developer.android.com/reference/android/os/UserManager.html\" rel=\"noreferrer\"><code>UserManager</code></a> class I came across the following method:</p>\n\n<blockquote>\n<pre><code>public boolean isUserAGoat()\n</code></pre>\n \n <p>Used to determine whether the user making this call is subject to teleportations.</p>\n \n <p>Returns whether the user making this call is a goat.</p>\n</blockquote>\n\n<p>How and when should this be used?</p>\n"
}, {
"tags" : [ "java", "dictionary", "collections", "iteration" ],
"owner" : {
"account_id" : 3336,
"reputation" : 39323,
"user_id" : 4807,
"user_type" : "registered",
"accept_rate" : 43,
"profile_image" : "https://www.gravatar.com/avatar/7453d8a240de4b8d26869aa92743bb8e?s=256&d=identicon&r=PG",
"display_name" : "iMack",
"link" : "https://stackoverflow.com/users/4807/imack"
},
"is_answered" : true,
"view_count" : 3569328,
"protected_date" : 1365576855,
"accepted_answer_id" : 46908,
"answer_count" : 46,
"score" : 4032,
"last_activity_date" : 1739820446,
"creation_date" : 1220649168,
"last_edit_date" : 1583649073,
"question_id" : 46898,
"content_license" : "CC BY-SA 4.0",
"link" : "https://stackoverflow.com/questions/46898/how-do-i-efficiently-iterate-over-each-entry-in-a-java-map",
"title" : "How do I efficiently iterate over each entry in a Java Map?",
"body" : "<p>If I have an object implementing the <code>Map</code> interface in Java and I wish to iterate over every pair contained within it, what is the most efficient way of going through the map?</p>\n\n<p>Will the ordering of elements depend on the specific map implementation that I have for the interface?</p>\n"
} ],
"has_more" : true,
"quota_max" : 10000,
"quota_remaining" : 9957
}