When will dynamic branch prediction be useful? [duplicate] - cpu-architecture

This question already has answers here:
Why is processing a sorted array faster than processing an unsorted array?
(26 answers)
Closed 4 years ago.
For static branch prediction one always assume that the branch is not taken, while for dynamic branch prediction if the branch is taken before then it is more likely to be taken again. But I cannot come up with a situation that this is useful? What application will benefit from this? Why not just use static branch prediction?

boolean b = compute something;
:
for (int j=0; j<1000000; j++)
if (b) one statement;
else another statement;
The if will cause a conditional branch with the same taken/not taken result each time, but that may vary from one run to the next.
(I know one could write that code a little better with the if controlling a couple of if-free for-loops, but that's not the point here)

Related

Drools LHS How to update map [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
Sorry for my bad expression. Actually I asked one of my friends who majored in English but knew little about programming to revise my posting. And it turns out that the problem makes less sense, though. I've conducted some research and realized that I should learn more about Drools for my next asking. Thanks for your patience, and the question will be deleted.
I've created a simple reproducer to understand better what's happening. You can find the project that fires both rules here. I always suggest trying to create a simple reproducer with the Drools archetype because it's easier to understand what's happening.
Having said that, your second rule is not firing because the condition this["ISELSE"] != "TRUE" is not satisfied. In fact you can see you put the value of the map to true in the first consequence.
$map.put("ISELSE", "TRUE");
A few other details:
You shouldn't do pattern matching on such a generic type such as java.util.Map. Use a specific type that wraps a Map. Be as specific as possible in your pattern matching constraints.
Also why using string booleans instead of java booleans? Drools supports all Java type system, and it's probably better to use better data types.
In the video you'll find some other suggestion such as using DebugAgendaEventListener to understand rules evaluation
Rules matching is indeterministic process, you can't predict LHS matching order.
Once matching process finished, rules are added to agenda queue for execution.
RHS execution order is indeterministic unless you use salience. Rules with bigger salience will be executed first.
If a rule changes state of the object while execution, next queued rule will be removed from agenda queue if LHS no longer matches.
If I understood you right, you need salience 1 for rule-2

Cycle iterator in Swift [duplicate]

This question already has answers here:
The elegant solution for function that gives everytime called different element of array(ordered and starting from new when all returned)?
(4 answers)
Closed 3 years ago.
In Python, there is itertools.cycle, which takes an iterable and makes an iterable iterator that repeatedly yields the contents from the source.
I would like to replicate this behavior in Swift.
A candidate for replicating this behavior would be the standard library's repeatElement(_:count:): doing repeatElement(seq, count: 5).flatMap({$0}) creates an array of the elements of seq five times, but this does not meet my requirements as it only repeats seq a finite number of times. Additionally, it creates an Array of length 5 * seq.length, where only a cache of the length of seq is actually needed.
So the question is: how can I create an infinite Sequence by repeating the elements of a source Sequence? The solution should not have a space cost more than O(n). (A O(1) would be impossible to guarantee in Swift, as a Sequence makes no guarentee that it can be iterated multiple times.)
What's wrong with manually implementing a Sequence backed by some array access modulo its length? With a bit of care your index need never overflow, and you can cycle on for as long as you like.
Note that the API docs warning reads more like a friendly reminder that re-use isn't a mandatory part of the Sequence interface contract. But that doesn't exclude your particular implementation from being reusable between multiple loops.

Swift - Set vs Array [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 6 years ago.
Improve this question
On one hand I want an ordered collection, on the other hand I want every item in the collection to appear only once.
I can either use an array and sort it every time I insert an item - and insert only if not in the array.
or use a Set data structure and sort it every time i query the data
Does someone have better solution?
There are several third-party libraries implementing an ordered set in Swift, so you could check them out.
Also, you could write your own implementation of an ordered set (you can base it on an existing one) if it is not an overkill for your task. The way you choose really depends on your app.
And in the end, you could use one of two ways that you proposed: using a built-in array or a set. In order to choose between them, take a look at your app: what action will be performed more often? Getting an access to elements in order (use array then) or addition/deletion of existing elements (probably, the set is the way to go).
This part was edited based on comments below
If you go for an array, note, that a built-in contains for arrays will not know that an array is sorted, so it will probably be O(N), not O(log(N)). So you should either write a custom replacement for the contains method, or (this is, once again probably a better way), write a custom collection class that implements contains the right way (however, since contains is a protocol extension method of SequenceType, my knowledge of Swift, I'm afraid, is not good enough yet to tell you how to do it properly, maybe someone else will).
UPDATE (based on your comment to your question):
I believe, in your particular case (a chat app) array is superior. You only have to sort old messages once, and you will not probably try to add very old messages once again, you only have to make sure you don't add new messages twice (it is implementation-dependent though, so you know better, I'm just assuming). So you only have to check that the last messages in your old array do not overlap with first messages in the array that you add. Sort of :)

Can optaplanner facts be arrays?

I'm looking to load a large number of facts, 3 million distances between points, and I want to know whether optaplanner facts can be provided using a searchable data structure such as an Array. My sense is that using a List to hold that many values would create long search times. I'm using Drools scoring.
Yes, problem facts can be arrays. Problem facts can also have fields that are arrays. To use them in the Drools score calculation, you need to return them from the getProblemFacts() method as a Collection, but just wrap them in an ArrayList, you will not suffer any noticable perf slowdown there: it's a 1 time hit at initialization and as soon as they are in the Drools workingMemory, that getProblemFacts() Collection is forgotten.
Note that ArrayList (which implements the interface List which in turn implements Collection) has the same memory and performance scalability characteristics of an array. What I mean by that is that it does have some overhead, but not BigO affecting overhead.
Also note that it's highly recommended to use new ArrayList(int initialCapacity) over the no-arg constructor if you have any estimation of the size you'll need.
Planning entities currently need to be a Collection because #PlanningEntityCollectionProperty currently only supports a return type of Collection. Feel free to make a jira that we also should support an array - or even contribute a github PR that implements it :)
Planning value ranges currently need to be a List or numeric bounds. Feel free to make a jira or PR that we also should support an array there, it's just a matter of copy-pasting ListValueRange to ArrayValueRange.

What distance metric for project source code makes sense? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
I want to compare the changes of the source code of two project stages, e.g. the web application source code before it was scalable, and the scalable one.
For me it is interesting to show how many lines needed to be changed, removed or added to get from one to the other stage. I'm searching for a good distance metric that rewards less code and little code changes - the one I imagine would output a relative value:
0% = "Both projects are the same"
50% = "Half of the source code has been changed"
100% = "Both projects have nothing in common"
Intentionally I came up with a few solutions:
diff: Maybe concat all files to a single source code file and run a diff against them. Problem here is that less code is better, but with this solution is counted as a plain change therefore punishing code removal.
Levenshtein Distance: Calculates the changes needed to transform source code a to source code b. The result is a number of changes in characters. Problem here again is, that code removal is not rewarded but punished.
Unified Code Count: Sets up rules how to consistently count lines of code, but is no descriptive distance metric between projects.
So I'm searching for a metric that is descriptive, rewards code removal and only counts in code changes or additions. It doesn't have to be source code specific, both projects use the same language. My personal feeling goes into the diff direction but I did not come up with a satisfactory descriptive metric.
What would you propose?
If you want, you can make this into a really difficult research problem:
http://gate.ac.uk/sale/dd/related-work/tao-related/2007+Kagdi+Survey+for+mining+software+repositories.pdf
This approach: http://www.cs.kent.edu/~jmaletic/papers/icsm04.pdf looks like it's under active development here: http://www.srcml.org/ .
There's other, more general, code metrics tools listed here http://www.aniche.com.br/wp-content/uploads/2013/04/scam2013.pdf (though it looks like the tool advertised by the paper is down now). Apparently Sonar has the ability to look at metrics over time: https://en.wikipedia.org/wiki/SonarQube