range-checking

Is it more efficient to perform a range check by casting to uint instead of checking for negative values?

我与影子孤独终老i 提交于 2019-11-28 19:03:00
I stumbled upon this piece of code in .NET's List source code : // Following trick can reduce the range check by one if ((uint) index >= (uint)_size) { ThrowHelper.ThrowArgumentOutOfRangeException(); } Apparently this is more efficient (?) than if (index < 0 || index >= _size) I am curious about the rationale behind the trick. Is a single branch instruction really more expensive than two conversions to uint ? Or is there some other optimization going on that will make this code faster than an additional numeric comparison? To address the elephant in the room: yes, this is micro optimization,

How to determine a date in between Friday and Sunday of the week at a particular time

微笑、不失礼 提交于 2019-11-28 12:44:03
问题 I'm trying to check a current date and time is in between Friday 17:42 and Sunday 17:42 of the week with Java. At the moment I'm doing this with really really bad code block. It was a hurry solution. Now I'm refactoring but I couldn't find any method in joda or etc. Any ideas? Thanks private final Calendar currentDate = Calendar.getInstance(); private final int day = currentDate.get(Calendar.DAY_OF_WEEK); private final int hour = currentDate.get(Calendar.HOUR_OF_DAY); private final int minute

Check if an Edit Text that only accepts number is not empty and the number is equal or less than 100

早过忘川 提交于 2019-11-28 06:24:41
问题 I'm building an application for receiving grades and I want to make sure that the Edit Texts are not empty and the values are less or equal to 100 I wrote this line but it crashes the application if(Integer.parseInt(editText.gettext().toString()) > 100 || editText.getText().toString().trim().length() == 0) { //Error message for example } and this is the logCat 09-04 18:21:06.331 8649-8649/com.example.nima.myapplication E/AndroidRuntime﹕ FATAL EXCEPTION: main Process: com.example.nima

Switch off Delphi range checking for a small portion of code only

心不动则不痛 提交于 2019-11-27 16:05:35
问题 How can one switch off range checking for a part of a file. Switching off is easy, but how do I revert to the project setting later on? The pseudo-code below should explain it: Unit1; //here's range checking on or off as per the project setting code here... {$R-} //range checking is off here because the code causes range check errors code here... //now I want to revert to the project setting. How do I do that? code here... end. 回答1: See: IFOPT directive. {$IFOPT R+} {$DEFINE RANGEON} {$R-} {

Is it more efficient to perform a range check by casting to uint instead of checking for negative values?

老子叫甜甜 提交于 2019-11-27 11:50:27
问题 I stumbled upon this piece of code in .NET's List source code: // Following trick can reduce the range check by one if ((uint) index >= (uint)_size) { ThrowHelper.ThrowArgumentOutOfRangeException(); } Apparently this is more efficient (?) than if (index < 0 || index >= _size) I am curious about the rationale behind the trick. Is a single branch instruction really more expensive than two conversions to uint ? Or is there some other optimization going on that will make this code faster than an