Free Essay

This Is Doo Doo

In:

Submitted By zeroef99
Words 780
Pages 4
Now for the sake of argument, suppose this is back in the 1800s - before long distance or radio communication.

You are the operator of a junction and you hear a train coming. You have no idea which way it will go. You stop the train to ask the captain which direction he wants. And then you set the switch appropriately.

Trains are heavy and have a lot of inertia. So they take forever to start up and slow down.

Is there a better way? You guess which direction the train will go!

If you guessed right, it continues on.
If you guessed wrong, the captain will stop, back up, and yell at you to flip the switch. Then it can restart down the other path.
If you guess right every time, the train will never have to stop.
If you guess wrong too often, the train will spend a lot of time stopping, backing up, and restarting.

Consider an if-statement: At the processor level, it is a branch instruction:

You are a processor and you see a branch. You have no idea which way it will go. What do you do? You halt execution and wait until the previous instructions are complete. Then you continue down the correct path.

Modern processors are complicated and have long pipelines. So they take forever to "warm up" and "slow down".

Is there a better way? You guess which direction the branch will go!

If you guessed right, you continue executing.
If you guessed wrong, you need to flush the pipeline and roll back to the branch. Then you can restart down the other path.
If you guess right every time, the execution will never have to stop.
If you guess wrong too often, you spend a lot of time stalling, rolling back, and restarting.

This is branch prediction. I admit it's not the best analogy since the train could just signal the direction with a flag. But in computers, the processor doesn't know which direction a branch will go until the last moment.

So how would you strategically guess to minimize the number of times that the train must back up and go down the other path? You look at the past history! If the train goes left 99% of the time, then you guess left. If it alternates, then you alternate your guesses. If it goes one way every 3 times, you guess the same...

In other words, you try to identify a pattern and follow it. This is more or less how branch predictors work.

Most applications have well-behaved branches. So modern branch predictors will typically achieve >90% hit rates. But when faced with unpredictable branches with no recognizable patterns, branch predictors are virtually useless.

Further Reading: http://en.wikipedia.org/wiki/Branch_predictor

As hinted from above, the culprit is this if-statement:

if (data[c] >= 128) sum += data[c];
Notice that the data is evenly distributed between 0 and 255. When the data is sorted, roughly the first half of the iterations will not enter the if-statement. After that, they will all enter the if-statement.

This is very friendly to the branch predictor since the branch consecutively goes the same direction many times. Even a simple saturating counter will correctly predict the branch except for the few iterations after it switches direction.

Quick visualization:

T = branch taken
N = branch not taken

data[] = 0, 1, 2, 3, 4, ... 126, 127, 128, 129, 130, ... 250, 251, 252, ... branch = N N N N N ... N N T T T ... T T T ...

= NNNNNNNNNNNN ... NNNNNNNTTTTTTTTT ... TTTTTTTTTT (easy to predict)
However, when the data is completely random, the branch predictor is rendered useless because it can't predict random data. Thus there will probably be around 50% misprediction. (no better than random guessing)

data[] = 226, 185, 125, 158, 198, 144, 217, 79, 202, 118, 14, 150, 177, 182, 133, ... branch = T, T, N, T, T, T, T, N, T, N, N, T, T, T, N ...

= TTNTTTTNTNNTTTN ... (completely random - hard to predict)
So what can be done?

If the compiler isn't able to optimize the branch into a conditional move, you can try some hacks if you are willing to sacrifice readability for performance.

Replace:

if (data[c] >= 128) sum += data[c]; with: int t = (data[c] - 128) >> 31; sum += ~t & data[c];
This eliminates the branch and replaces it with some bitwise operations.

(Note that this hack is not strictly equivalent to the original if-statement. But in this case, it's valid for all the input values of data[].)

Benchmarks: Core i7 920 @ 3.5 GHz

C++ - Visual Studio 2010 - x64 Release

// Branch - Random seconds = 11.777

// Branch - Sorted seconds = 2.352

Similar Documents

Premium Essay

Review Of Lou Reed's Song 'Walk On The Wild Side'

...Always intertwined with current events and signs of the times, music has long been a telling relation with the society from which it is created. At any point in history, one can usually look to the music of that time to reflect what is going on in the world. This holds especially true for the Civil Rights era in the United States. In Lou Reed’s song “Walk on the Wild Side,” he sings the following at the chorus: “Said, hey babe, take a walk on the wild side And the colored girls go Doo doo doo doo doo doo doo doo doo Doo doo doo doo doo doo doo doo doo” (Lou Reed, 1972, Side A). These are the lyrics can be heard in Morgan Neville’s documentary “20 Feet from Stardom” (Leight, 2013). This sets the tone for the rest of the documentary– an exploration...

Words: 875 - Pages: 4

Premium Essay

Personal Narrative: From My Family To Colorado

...meet up with our, aunt, uncle, and 2 older cousins at pueblo reservoir. A day or two after we were told this we were woken up early so we could get to pueblo and enjoy the whole day there. We had a quick breakfast, packed some stuff for a barbeque, and piled into the massive brick of an SUV ,otherwise known as a Suburban, and we headed out, on the way I asked a few questions that any five year old would undoubtedly ask on a trip like where we were going, if we were there yet, and a few others....

Words: 657 - Pages: 3

Premium Essay

Research Paper

...it when the moon looks like a toenail And I love you when you say my name If you wanna know Here it goes Gonna tell you this The part of me that'll show if you're close Gonna let you see everything But remember that you asked for it I'll try to do my best to impress But it's easier to let you take a guess at the rest But you wanna hear what lives in my brain My heart, will you ask for it, for your perusing? At times confusing, slightly amusing Introducing me Doo doo, doo doo doo doo to Doo doo, doo doo doo doo to La la la la La la la la la la la la, da I never trust a dog to watch my food And I like to use to the word "dude" As a noun, or an adverb, or an adjective And I've never really been into cars I like really cool guitars and superheroes And checks with lots of zeros on 'em I love the sound of violins And making someone smile If you wanna know Here it goes Gonna tell you this The part of me that'll show if you're close Gonna let you see everything But remember that you asked for it I'll try to do my best to impress But it's easier to let you take a guess at the rest But you wanna hear what lives in my brain My heart, will you ask for it, for your perusing? At times confusing, possibly amusing Introducing me Well, you probably know more than you ever wanted to So be careful when you ask next time So if you wanna know Here it goes Gonna tell you this The part of me that'll show if you're close Gonna let you see everything But remember that you asked for it I'll try to...

Words: 5900 - Pages: 24

Premium Essay

The Art of Peer Pressure by Kendrick Lamar

...The Art of Peer Pressure by Kendrick Lamar It is necessary that I begin with disclosing the fact that this song is just an extract of an album that describes the reality of a portion of our society that, unfortunately, is in much more need of help than any of us care to realize. In his album good kid, m.A.A.d city, Kendrick Lamar tells a first person story of a day in the life of a “crew” of young gangbangers from Compton, CA. The album is full of songs that capture specific thoughts, feelings or provide background to what is happening in the story at that particular moment. The transitions between songs present multiple voice recordings from Kendrick’s and his mother’s voicemail. These transitions are part of the album’s chronological order and are vital in attaining a complete personality and mental profile of the characters followed throughout the story. Simply, the songs and transitions are each just a part of a “bigger picture”. There is one song, though, that tells a particular story which I think accounts for the largest piece of this “bigger picture”. The Art of Peer Pressure K.dot (Kendrick Lamar) and three of his blood are driving around Rosecrans in a white Toyota. K.dot (16) is the youngest member of the crew and the oldest is 23. His mother is concerned that he will not get to 11th grade if he keeps “messing around with those damn hoodrats”. This is established in a previous song, but it is indicative that his performance in school has dropped as of recently...

Words: 2186 - Pages: 9

Premium Essay

Compare And Contrast Scozyby Do

...For my comic I chose Scooby Doo. I looked at a comic that is newer and one that is older and they haven’t changed much. The main characters are all the same and they look the same. I also think they’re similar because they have the same voice, each of the characters have certain way of saying things that are different. Something that is very similar for both is the simplicity of the message. The stories in the comics are very well portrayed in only four to eight panels usually. The story line is never too complicated so that the readers can get through the comics quickly and be able to understand it. I think they did this because a large portion of the readers are children. All of the characters are very different. They all have their...

Words: 259 - Pages: 2

Free Essay

Sociology - Gender

...relations in the male and female category. One major subject of gender is gender roles, which basically means how males and females should speak, think, dress and act within the context of society and can be linked to gender stereotyping (also a topic of discussion). It is apparent in most if not all cultures, but the “role” itself varies from one culture to another. A major gateway for this is through the media. In the United States, children’s cartoons are a prime example. Of the plethora of cartoons available for discussion, four highly popular ones, current and old, will take priority: Dora the Explorer, Spongebob Squarepants, Rugrats, and the Scooby Doo series. First to be discussed is the extremely popular, Dora the Explorer. The show can be considered a global phenomenon being dubbed in over twenty different languages, so it has a large fan base amongst the varying cultures around the world. Now, on to the topic of gender in regards to the show, the show itself is not necessarily targeted solely towards young females/girls. In fact, the show is highly popular amongst young males/boys as well. This is mainly due to the fact that Dora isn’t your typical “girl” in the sense of gender roles, which she is more or less considered a tomboy. Dora wears a t-shirt, shorts, and tennis shoes and carries a backpack; she also has short hair. Also, the context of the show helps lean interest and...

Words: 1432 - Pages: 6

Free Essay

Journals Unit 5 and 6

...between episodes?   Are there reoccurring locations, or is there a new setting for the show each week?  If your show is different in these ways from Golden Age cartoons, why do you think that is?   In the last three episodes of my tv show, Scooby-Doo, the characters don’t change or grow in their characters but they do become more eager and impatient in finding more mysteries and getting them solved. For example, in one of the episodes Velma, Daphne, Fred, Shaggy and Scooby-Doo are all sitting around Fred’s house bored and ready for another mystery to land in their hands. Velma says “I’m so bored, we haven’t had a good mystery in I don’t know when.” This is usually the case in most episodes. The gang is anxiously waiting for another mystery to solve when one comes straight into their hands. In these episodes each mystery is occurring in different locations. One is occurring in Gatorsburg, the second in their hometown, and another on the beach. I don’t think the show I’m watching is that much different than ones from the Golden Age because they both have a somewhat violent context to the episodes. Some of the episodes in the Golden Age involve two characters arguing or fighting to get something they want. I think it’s the same in this tv show because the gang are opposing someone else and solving the mystery in order to get the...

Words: 665 - Pages: 3

Free Essay

Căn Bản Php

...(server script) chaïy treân phía server (server side) nhö caùch server script khaùc (asp, jsp, cold fusion). PHP laø kòch baûn cho pheùp chuùng ta xaây döïng öùng duïng web treân maïng internet hay intranet töông taùc vôùi moïi cô sôû döõ lieäu nhö mySQL, PostgreSQL, Oracle, SQL Server vaø Access. Löu yù raèng, töø phieân baûn 4.0 trôû veà sau môùi hoã trôï session, ngoaøi ra PHP cuõng nhö Perl laø kòch baûng xöû lyù chuoãi raát maïnh chính vì vaäy baïn coù theå söû duïng PHP trong nhöõng coù yeâu caàu veà xöû lyù chuoãi. 1. GIÔÙI THIEÄU PHP 2. CAØI ÑAËT PHP Caøi ñaët PHP treân neàn Windows thì söû duïng php-4.0.6-Win32.zip, sau khi caøi ñaët öùng duïng naøy treân ñóa cöùng seõ xuaát hieän thö muïc PHP, trong thöï muïc naøy seõ coù taäp tin php4ts.dll vaø php.exe cuøng vôùi thö muïc sessiondata. Ngoaøi ra, trong thö muïc WINDOW hoaëc WINNT seõ xuaát hieän taäp tin php.ini, taäp tin naøy cho pheùp baïn caáu hình cho öùng duïng PHP. Chaúng haïn, khi söû duïng session, PHP caàn moät nôi ñeå löu tröõ chuùng, trong taäp tin naøy maëc ñònh laø session.save_path = C:\PHP\sessiondata, neáu baïn caøi ñaët PHP vôùi thö muïc PHP treân ñóa D thì baïn caàn thay ñoåi ñöôøng daãn trong khai baùo naøy. Töông töï nhö vaäy, khi coù loãi trong trangPHP thì loãi thöôøng xuaát hieän khi trieäu goïi chuùng,...

Words: 7561 - Pages: 31

Premium Essay

The Beatles - Happiness Is a Warm Gun Analysis

...the song ensured a lot of time in the recording studio. They spent two days in studio recording a total of 70 takes with such instruments as organ, piano, tambourine and bass in support. Form Instead of following a traditional song format, such as a verse – chorus – bridge, the song is organised into four distinctive parts, each with its own genre, subject matter, tempo, metre, and chord progression (Connolly, 2013). First Part: “She’s Not a Girl Who Misses Much...” 0:00-0:44 Part One beings with around 14 seconds of finger picking in 4/4 time and a rather somber sounding vocal line. There is a sudden increase in intensity and transposition by drum crescendo into fuzz guitar and continued drumming creating a psychedelic rock feel. This second part is trickier to decipher, it sounds like a bar of 4/4, followed by a bar of 2/4, back to five bars of 4/4 then perhaps a bar of 5/4. The lyrics continue, using surreal imagery with such lines as “..a soap impression of his wife which he ate..”. The finger picking pattern in the first 14 seconds was quite popular with the Beatles at that time. It features in other songs from the same album, such as “Dear Prudence”. Second Part: “I need a fix...” 0:44-1.13 Part Two is introduced by a Bluesy guitar rift which thanks to the use of triple meter (three beats to the bar) moves into a sort of bluesy waltz (Krerowicz, 2013). The singer laments his drug addiction “I need a fix cause I’m going down, down to the...

Words: 866 - Pages: 4

Free Essay

Water

...UltraSoft 150 625.388160 Caution: Read and Follow All Safety Rules and Operating Instructions Before First Use of This Product. If you have questions when installing, operating or maintaining your softener, and when setting the timer, call this toll-free number... Water Softeners with Deluxe Valve x Warranty x Start Up / Setting Timer x How It Works x Care Of x Specifications x Repair Parts SAVE THIS MANUAL Use the plastic bag and tie provided, to hang manuals nearby the softener for future reference. 1-800-426-9345 www.KenmoreWater.com 6HDUV 5RHEXFN DQG &R  %HYHUO\ 5RDG +RIIPDQ (VWDWHV ,/  86$ 35,17(' ,1 86$ WARRANTY W A R R A N T Y SEARS RESIDENTIAL WATER SOFTENER FULL ONE YEAR WARRANTY ON WATER SOFTENER For one year from the date of purchase, when this water softener is installed and maintained in accordance with our instructions, Sears will repair, free of charge, defects in material or workmanship in this water softener. FULL TEN YEAR WARRANTY AGAINST LEAKS For ten years from the date of purchase, Sears will furnish and install a new current model water softener tank or salt storage drum, free of charge, if either the tank or drum develop a leak. TO OBTAIN WARRANTY SERVICE, SIMPLY CONTACT THE NEAREST SEARS SERVICE CENTER THROUGHOUT THE UNITED STATES. This warranty applies only while this product is in use in the United States. This warranty gives you specific legal rights, and you may have other rights which vary from state to state. Sears, Roebuck...

Words: 9808 - Pages: 40

Premium Essay

Hardship Letter

...Re: Rosaura Munoz Juan Munoz To whom this may concern, I, Jun, Dee’s younger sister, am writing on behalf of Dee and Doo regarding her immigration status. My sister was brought into the United States by our mother and father when she was a toddler. I am aware of the fact that they entered the U.S. territory illegally, but inspite of their actions; they were only in search of a better economic lifestyle. Dee has lived her entire life in the United States and has never returned to Mexico since then. If you were not to grant her permission to return back with us, many things will drastically be affected, like her husband, their children, and her life. Doo has been an amazing brother-in-law. I am so proud of all of his accomplishments throughout his life serving in our Army to defend our nation. He already has to deal with many things regarding his job, and the thought of taking his wife out of his life has only made it more stressful for him. I could not imagine Doo’s life without my sister being a part of it. Secondly, my nephew and niece would be devastated to hear their mother cannot return to their home. Every child needs a mother’s warmth to feel safe and loved. Separating them from Dee can have a negative impact for their futures. Let her have the opportunity to see her children become outstanding human beings by their side. Finally, Dee has been an outstanding person that has left a trail of unforgettable memories that cannot possible be forgotten. She...

Words: 438 - Pages: 2

Premium Essay

The Bean Trees Analysis

... While spending time at what they call “Dog Doo Park”, Taylor and Lou Ann notices the wisteria vines are starting to bloom. They are both pleasantly surprised, as Taylor states, “It reminded me of that Bible story where somebody or other struck a rock and the water poured out. Only this was better, flowers out of bare dirt. The Miracle of Dog Doo Park” (152). An expression like this is important to the story since it describes one of the main themes of the book, having faith in friends. Lou Ann has always promised Taylor the usually brown wisteria vines would bloom, but Taylor has always been skeptical. Lou Ann keeps her promise, when one day they find wisteria vines blooming out of the ground at Dog Doo Park. Taylor calls it a miracle, but Lou Ann knew it would happen all along. This is just one of the many examples when Taylor has faith in a friend, and it ends up beneficial for her. For instance, Taylor looks to Mattie for help with her car, and finds a lifelong friend full of wisdom. Secondly, Taylor has faith in Estevan and Esperanza when she decides to take them north of the border to a safer place. She knows they were trustworthy, smart, and would be able to avoid trouble if necessary. Kingslover incorporates a metaphor through Taylor’s statement, comparing a popular biblical story to the miracle Taylor and Lou Ann found at Dog Doo Park. Overall, this...

Words: 1888 - Pages: 8

Free Essay

Student

...Someday mai roo waa naan kae nai tee chun dtong ton gup took sing bit bung kwaam jing nai jai took took yaang took krung tee rao pop gun took krung tee ter hun maa tee chun cher-ee cher-ee roo mai chun feun kae nai dai yin mai hua jai chun mun gum-lung bork ruk ruk ter yoo dtae chun mai art ja bert pia jai ork bai hai krai dai roo dai yin mai hua jai chun yung koi yoo dtrong nun ror hai ter bert doo lae wung piang kae ter roo suk wun neung tung tee chun gor ruk tung tee chun gor roo seuk dtae suan leuk kaang nai yung mai glaa took krung tee rao pop gun took krung tee ter hun maa tee chun cher-ee cher-ee roo mai chun feun kae nai dai yin mai hua jai chun mun gum-lung bork ruk ruk ter yoo dtae chun mai art ja bert pia jai ork bai hai krai dai roo dai yin mai hua jai chun yung koi yoo dtrong nun ror hai ter bert doo lae wung piang kae ter roo suk wun neung dai yin mai hua jai chun mun gum-lung bork ruk ruk ter yoo dtae chun mai art ja bert pia jai ork bai hai krai dai roo dai yin mai hua jai chun yung koi yoo dtrong nun ror hai ter bert doo lae wung piang ter ja roo waa kon kon nee ruk ter yoo yung kor hai ter roo suk wun neung I don't know how much longer that I have to put up with you I've been hiding everythings in my heart Everytime we meet each other, Everytime we face each other Though I am indifferent.. Do you know how much have I have to force myself? Can't you hear my heart calling for you, loving you But I can't release my heart out for anyone...

Words: 496 - Pages: 2

Free Essay

Cn1 - Cisco Router Vietnamese

...Höôùng daãn caáu hình caùc chöùc naêng cô baûn cuûa Cisco router HÖÔÙNG DAÃN CAÁU HÌNH CAÙC TÍNH NAÊNG CÔ BAÛN CHO CISCO ROUTER MUÏC LUÏC 1 Khaùi nieäm veà Router .................................................................................................. 3 1.1 Nhieäm vuï vaø phaân loaïi......................................................................................... 3 Nhieäm vuï: ..................................................................................................... 3 Phaân loaïi....................................................................................................... 3 1.1.1 1.1.2 1.2 Caùc khaùi nieäm cô baûn veà Router vaø cô cheá routing............................................. 5 Nguyeân taéc hoaït ñoäng cuûa Router – ARP Protocol: ..................................... 5 Moät soá khaùi nieäm cô baûn............................................................................... 7 1.2.1 1.2.2 2 Khaùi nieäm veà caáu hình Router. ................................................................................. 13 2.1 2.2 Caáu truùc router. ................................................................................................. 14 Caùc mode config ............................................................................................... 16 3 Caáu hình caùc tính naêng chung cuûa router. ............................................................... 19 3.1 3.2 Moät soá quy taéc veà trình baøy caâu leänh...

Words: 25755 - Pages: 104

Free Essay

Market Analysis Japan

...2nd largest economy based on gross domestic product which stands at $34,000.00 per capita. The unemployment rate is one of the lowest at 4%. This indicates that there is a good standard of living, most of the population are employed, thus the population has disposable income and can spend on luxury items such as sea-doos. Japan has a strong currency which trades 1 JPY for 0.121 CAD. The economic stability and strong currency provides a sound investment platform for global businesses. Target Market Size: Japan has an overall population of 127,078,679. 64.3% of the population are between the ages of 15 to 64, which incorporates our target market age group. The 64.3% represents approximately 41,187,425 males and 40,533,876 females. Moreover, 66% of the total population are urban dwellers which are ideal for our product. A highly urban population eases promotional tasks by having access to the population through major cities rather than rural area. Out of the approximately 127.5 million people who reside in Japan, 99% are literate and 66.4% work in the service industry. This goes to show us that their employees are well educated and generally earn a good income which goes to show they can afford to spend more. According to a recent survey by the World Tourism Organization, the Japanese take 25 days per year off for vacationing. This is ideal for Bombardier, as our water crafts are primarily going to be used during vacation time (personal use or through resort). In terms...

Words: 2706 - Pages: 11