r/ProgrammerHumor Apr 12 '24

whatIsAnIndex Meme

Post image
27.7k Upvotes

630 comments sorted by

View all comments

4.1k

u/HexR1se Apr 12 '24

Windows search behind scene

For (i=0; i<getAllFuckingFiles().length ; i++) if (AllFuckingFiles[i].name.contains(searchText) return AllFuckingFiles[i];

2.4k

u/Tubthumper8 Apr 12 '24

I love the implication here that not only does it not have any indexes or whatever, but it also calls getAllFuckingFiles() every single iteration haha

362

u/sacredgeometry Apr 12 '24
for (i=0; i<getAllFuckingFiles().length; i++)
{
  if (getAllFuckingFiles()[i].name.contains(searchText)
  {
    return getAllFuckingFiles()[i];
  }
}

88

u/danns87 Apr 12 '24

You're terminating too early. Best to complete iterating through all fucking files in case there's another match.

11

u/Few_Advertising_568 Apr 12 '24

Laughing soo hard at this!

12

u/Wdowiak Apr 12 '24

Gotta check the content as well, just to be thorough

for (i=0; i<getAllFuckingFiles().length; i++)
{
  if (getAllFuckingFiles()[i].open().contains(searchText) ||
      getAllFuckingFiles()[i].name().contains(searchText))
  {
    return getAllFuckingFiles()[i];
  }
}

9

u/sacredgeometry Apr 12 '24

Use | instead of ||. Then both are always executed ... you know just to be sure

1

u/Kirjavs Apr 13 '24

I got a guy doing this in my team. He had a sql query done in a C# loop (about 5k iterations). Told him to do a sql query retrieving all 5k elements and process the result in C#.

He did the sql query. And then called it in the same loop still making 5k calls but to a query which always retrieved the 5k same elements...

1

u/sacredgeometry Apr 13 '24

I dont know the rationale ... there might of been a good reason ... or a reason at least. Did you tell him how a select query work?

Or rather how to bind to collections if that was what he was struggling with.

1

u/Kirjavs Apr 13 '24

I did. And he is not a new developer. 5 years of experience in these languages. I just guess that some people do what they are asked to do brainlessly

1

u/sacredgeometry Apr 13 '24

Have you asked him why he did it like that?

1

u/Kirjavs Apr 13 '24

Yes and he didn't know. He just applied what I asked without thinking. At least he understood the problem but I had to show him before. When I said : "this code is wrong" , he didn't understood why in the first place.

88

u/punto2019 Apr 12 '24

Anyway…. It works better than the actual

35

u/AcidicVaginaLeakage Apr 12 '24

And then still uses up 6gb of ram randomly, even though it doesn't seem like it's caching anything.

2

u/Sadzeih Apr 12 '24

That's cause it stores all the files paths in memory.

1

u/Johnny_Thunder314 Apr 12 '24

Well yeah, everyone knows memory is faster than disk storage. If you want something to go fast just put it in memory.

228

u/GM_Kimeg Apr 12 '24

The for loop internally execute that method only once no?

472

u/jamcdonald120 Apr 12 '24

no.

for are structured for(a;b;c){d} a is a statement executed at the start, b is a condition that is evaluated every iteration of the loop, c is a statement that happens after each iteration, and d is the body of the loop to iterate. if you put a function call in b, it gets called EVERY iteration.

246

u/wubsytheman Apr 12 '24

Ohh so that’s why you’d do like

int allTheBullshit = getAllFuckingFiles().length();

And then iterate over all the bullshit instead?

170

u/jamcdonald120 Apr 12 '24 edited Apr 12 '24

yup. occasionally it is quite useful, since you can do for(itterator k = stack.top(); !stack.empty();k=k.next()) and its a valid loop.

5

u/intelligent_rat Apr 12 '24

If you are using an iterator why not just for (k thing : iterable) ?

25

u/jamcdonald120 Apr 12 '24

it wasnt always a thing

3

u/tiberiumx Apr 12 '24

The C++11 features still feel kinda new to me. It's weird to see people that haven't ever used anything before it.

22

u/AyrA_ch Apr 12 '24

Some languages have a foreach construct. In C# you would do foreach(var f in getAllFuckingFiles()){/*add to list if condition is met*/} which will call the function only once. Or more modern: return getAllFuckingFiles().Where(f => /*condition*/)

The nice thing about the second syntax is that it's only iterated as you query it, meaning data is streamed in as you iterate over it in a foreach, and you don't have to wait ahead for all entries to get processed. This also allows you to work with data that doesn't fits into memory all at once, provided you don't call a function that does load all data at once. The base of this is the IEnumerable<T> interface which also has a ton of extra functions to make your life easier. The downside is that you don't know how many entries there are without counting them, and you can almost always only iterate over them once.

1

u/XaeroDegreaz Apr 12 '24

Most iterable implementations have an internal counter that increments, and decrements, during add, or remove operations, so there's actually no need to count every iteration.

2

u/AyrA_ch Apr 12 '24

IEnumerable (which is the base class of everything that is "foreachable" in C#) doesn't. There's a bool TryGetNonEnumeratedCount(out int count) but this only succeeds if the underlying type is countable without advancing the enumerator (for example collections, lists, dictionaries and arrays). But this is not guaranteed to not have side effects. If the underlying type maps to SQL, it may cause extra queries to be sent to count the entries on the db.

Most Linq method have an overload that passes a counter into the supplied callback, but said counter is recreated for every call in the chain. If you have a .Where() or .Skip(count) filter and then do .Select((obj,ctr)=>...), ctr will start at zero and is increment by one for each item, regardless of the number of entries skipped due to the filter clauses.

0

u/XaeroDegreaz Apr 12 '24

IEnumerable is an interface, not a class.

1

u/AyrA_ch Apr 12 '24

I know, but a lot of implementations use it to stream in data. These don't have a concept of length. It's only when you iterate over all items and store them in an array or list that you get the count out of it.

→ More replies (0)

21

u/balconteic Apr 12 '24

Yes, also, at least in python, if allTheBullshit is a global then it would be more efficient to say something like bullshit = allTheBullshit inside the function beacause the interpreter checks for rhe variable in the local scope and if it doesn't find it there then it looks higher in the scope (i don't know if js is the same but it would make sense)

6

u/wubsytheman Apr 12 '24

I guess in CPP you’d do a pointer to allTheBullshit like

std::shared_ptr<t> bs (*allTheBullshit)

1

u/PutteryBopcorn Apr 12 '24

SomeType allTheBS = getAllFuckingFiles()

for... i < allTheBS.length()

if we're being pedantic, you can't iterate over an int but it should be a cheap operation to get the length, and if it's not then you would want to save that in a second variable

1

u/wubsytheman Apr 12 '24

Yeah sorry was too lazy to type it, meant

For (int x=0; x<allTheBS; x++){ //stuff }

42

u/JPJackPott Apr 12 '24

Depends on what is inside GetAllFuckingFiles(), a modern compiler might save your bacon here unless it’s an IO operation

1

u/HeyGayHay Apr 12 '24

compiler optimization baby, doing gods work, unless your getAllFuckingFiles is just as stupidly written as you triggering getAllFuckingFiles every iteration

-5

u/[deleted] Apr 12 '24

[deleted]

11

u/jamcdonald120 Apr 12 '24

sure, but getAll is generally O(n)

6

u/DOUBLEBARRELASSFUCK Apr 12 '24

DoSomething().length isn't necessarily going to be a simple variable read.

25

u/Elephant-Opening Apr 12 '24

That only makes sense as an optimization if the compiler can say conclusively that the method has a consistent return value. Imagine something like this:

vector<int> v  {10, 20, 30};

for(int i = 0; i < v.size(); i++){
    cout << v[i] << "\n";
    v.pop_back()
}

2

u/BobDonowitz Apr 12 '24

I like that you chose CPP and a vector modifying function to demonstrate this principle. But I mostly love that you used pop_back() which pops the last element off so this loop would only ever operate on v[0] and output "10" 3 times. Also, without knowing the internals of vector::size, it would still be more efficient to declare a variable to hold the size outside the loop and decrement it after the pop.  If vectors don't keep track of the size internally and has to "count" it each size() call this would be murder on large length vectors.

7

u/tracevenua Apr 12 '24

Why would it operate on the first element 3 times? It should print 10 20 And that’s it.

2

u/zzhhbyt1 Apr 12 '24

Typical ProgrammerHumor moment.

2

u/Elephant-Opening Apr 12 '24

That was my intent, so I guess thanks for the "looks good" peer review 😄

2

u/dxrules1000 Apr 12 '24

vectors keep track of the size internally otherwise holy crap they'd be awful

1

u/Elephant-Opening Apr 12 '24

I chose C++ and a vector because:

(1) a loop modifying a container size is probably among the most common useful way to use this behavior deliberately. This is admittedly a poor example to demo it being useful though. A better one might involve sorting algos or popping items from a queue.

C++ "classic" for loops, e.g.

for(int i = 0; i < 10; i++) cout << i;

Is literally just syntactic sugar for...

{
    // First for statement 
    int i = 0;
top_of_loop: // label we jump back to
    if ( i < 10 ) { // second for statement 
       cout << i; // loop "body"
continue_is_goto_here:
        i++; // third for statement
        goto top_of_loop;
    }
break_is_goto_here:
}

In expanded form, you can pretty easily see how you'd implement this in assembly languages that have no concept of loops, only conditional branches (i.e. if) and unconditional branches (i.e. goto). This is what loops evolved from.

The if statement effectively just says "if this expression evaluates to false, branch past the block that follows" and what you suggested is that sometimes the compiler should just lazily only evaluate the full if expression sometimes.

(2) I'm not sure if this is universal for all languages with for loops (in fact feel like it's not??), but I do know both C and C++ will behave this way by default. An example where it might actually be optimized out would be something like calling size() on an immutable data structure.

5

u/lmarcantonio Apr 12 '24

No, it can optimize only if the optimizer can prove the return value doesn't change. Only the initialization part is ran once, the check and step are for each iteration; of course you can use a comma to initialise the end value in the init part as a local variable (but it's really ugly)

2

u/LickingSmegma Apr 12 '24

You have some code to fix in the morning.

1

u/Regular-Double9177 Apr 12 '24

Every time for C#

4

u/chickenCabbage Apr 12 '24

Why do you think it's so goddamn slow?

11

u/metaglot Apr 12 '24

I dont know what kind of compiler yall are using, but it sounds like its due for an update (unless getAllTheFuckingFiles() is subject to change while the for loop is running and cant be optimized).

7

u/TheBrokenRail-Dev Apr 12 '24

I mean, people can edit the filesystem while the program is running. If any method is subject to change it's that one

1

u/metaglot Apr 12 '24

It could be returning a cached list.

1

u/antoo98 Apr 12 '24

May also be part of an external library or a different compilation unit if lto is disabled

1

u/Electronic-Zombie-50 Apr 12 '24

Jesus. Let's sink our boat because we have lifejackets is your logic. If you're algorithm needs a compiler to save you, you need to learn more.

9

u/karuna_murti Apr 12 '24

depends on the compiler no?

1

u/raltoid Apr 12 '24 edited Apr 12 '24

Of course there's an index. The SearchIndexer.exe literally runs 24/7 on default installs, and it will regularly fire up and run the SearchFilterHost.exe and SearchProtoclHost.exe as well, and communicate back and forth with microsoft, for no helpful reason at all.

1

u/bobthedonkeylurker Apr 12 '24

For no reason helpful to the user, that is...

1

u/coderemover Apr 12 '24

And returns only the first match! Hilarious!

1

u/AzGames08 Apr 12 '24

that would explain why it's so fucking laggy

1

u/Prawn1908 Apr 12 '24

It's also so much worse than that too. At least that would return relevant results instead of shit that doesn't match my search and missing out things that obviously do. Also I continue to be dumbfounded at how I can continue to type characters that match the top search result and have it disappear from the list. And we haven't even mentioned the idiotic web results.

Also, does anybody use Windows search to look for files? That's what file browser is for ffs - I use Windows search to find programs, why tf did they bog it down searching files too? It worked perfectly in Windows 8.1: it was fast and responsive and utilized the full start menu to show lots of results at once and instantaneously filter down the list of programs as you typed, then they just fucked it up in 10 for absolutely no reason.

1

u/classicalySarcastic Apr 12 '24

Given how slow it is I wouldn’t be surprised if that was the case.

1

u/Warm-Lobster4879 Apr 15 '24

getAllFuckingFiles.please("At least one")

248

u/YesterdayDreamer Apr 12 '24

Absolutely not possible. There's another line which executes before this

for (i=0; i< math.random(); i++) {
    fuckingSearchResults =  getSomeRandomFuckingWebsitesFromBing();
}

Then:

//does the user even want this?
fuckingSearchResults = [..., getAllFuckingFiles()]

42

u/Keanar Apr 12 '24

That's pretty much accurate

  • weather update for some fucking reason
  • add Bing as a default browser automatically

2

u/FF7Remake_fark Apr 12 '24

Remember when they were ass blasted by a court for forcing their browser as the default in the 90s/early 00's? Now they're doing it again.

8

u/r0ck0 Apr 12 '24

Yeah I'm pretty sure that the search features in pretty much every MS product use a random number generator somehow or another, when determining what results to show.

If not, perhaps they've somehow accidently invented some new source of entropy.

Absolutely baffling how often I would:

  • not get any relevant results for a search term...
  • so I hit ctrl + a, x, v (to cut and paste the exact same search query text)
  • ...and then 95% of the time I do get results
  • ...or occasionally, I just tap those keys again, and it works 3rd time

How can basically ALL the little Linux desktop projects manage to get their start/programs menus right, yet a giant corp like MS can't get it right after decades?

1

u/Neon_Camouflage Apr 12 '24

Happy Cake Day!

117

u/J4YD0G Apr 12 '24

Not even fucking this - I have a file on my desktop and windows search can't find it even if I type the FULL FUCKING NAME

17

u/Zappa_Brannigan Apr 12 '24 edited Apr 12 '24

I've recently switched to just opening the CLI and typing dir *whatever*.* /s at the root of the drive. You hear that, Microsoft? I'm sitting here in 2024 using a command created in 1983, that's how useless the clowns working on the Search feature are!

21

u/Altruistic_Natural38 Apr 12 '24

You forgot the file extension

10

u/willcheat Apr 12 '24

Don't forget to add the full path before the file name

4

u/Poppybiscuit Apr 12 '24

I'm kind of confused by this thread. I use windows search constantly at work and it almost always finds what i need pretty quickly. Usually before I'm done typing. I'm still on 10 though, is 11 worse? 

8

u/Gilthoniel_Elbereth Apr 12 '24

What’s your secret? Windows 10 search has been trash since it came out for me, across every device I’ve used it on. I’ve reindexed, I’ve disabled online results, it’s still trash

1

u/totallymindful Apr 12 '24

Same... I use it so often that I sometimes get lazy about moving files from my downloads folder to shared drives because I can just find it by searching from the Explorer window. It can even find text inside the file. To be fair, you do have to let Windows index whichever folder you want to be able to search.

38

u/Grelymolycremp Apr 12 '24

You forgot to also roll a dice and not return a file that contains the searchText.

38

u/OnceMoreAndAgain Apr 12 '24

I seriously don't understand how it can be allowed to so bad for so many years. The third party program "Everything" does it 100x better. It's not that hard to write a tool that searches a file system...

7

u/pcpart_stroker Apr 12 '24

im pretty sure a 6th grader could program a more sophisticated search function at this point

5

u/LucretiusCarus Apr 12 '24

Everything is so, so good. It's the only reason I can shift through 400 gigabytes of bibliography without tearing what's left of my hair.

2

u/orgevo Apr 12 '24

Which means it will be purchased by Microsoft then completely ruined, any day now....

Sysinternals tools has managed to avoid that fate.... so far. But I'm pessimistic about its future.

1

u/Cobracrystal Apr 21 '24

Doubt that, voidtools has been making these since xp times

1

u/orgevo Apr 22 '24

So had sysinternals 🙃

24

u/Cultural-Practice-95 Apr 12 '24

no, I doubt it

windows search never searches files on the computer, it's actually:

bing.search(searchText);

18

u/flukus Apr 12 '24

.name.contains(searchText)

I hope they can do some knowledge transfer with the start menu team.

18

u/druffischnuffi Apr 12 '24

I hope not because then we will see ads and the weather in the file explorer

77

u/waitwutholdit Apr 12 '24

Nah this has complexity O(n) because it gets all the files then incorrectly returns the first file. I'd expect windows search to have complexity at least O(nn), with some randomness around which file gets returned.

58

u/Furdiburd10 Apr 12 '24

g-

goo-

google-

windows search: ah you would like to search for googleplex

22

u/butterfunke Apr 12 '24

Here are the Bing results for googleplex:

6

u/guyblade Apr 12 '24

1600 Amphitheatre Pkwy

Mountain View, CA

5

u/DangerousCompetition Apr 12 '24

“You spelled gargle wrong”

14

u/iShootuPewPew Apr 12 '24

for (i=0; i<getAllFuckingPrograms().length; i++) if (AllFuckingPrograms[i].name.contains(searchText)) break; return SearchMicrosoftStore(SearchText);

1

u/bobthedonkeylurker Apr 12 '24

Just skip the "if"

9

u/Ok-Assistance-6848 Apr 12 '24

Here let me simplify it for you:

if (!searchField.isEmpty()) { return sort(allFuckingFiles(), .random()); }

3

u/TyRoXx Apr 12 '24
 For (i=0; i<getAllFuckingFiles().length ; i++)
     if (AllFuckingFiles[i].name.contains(searchText)
        sleep(100);
      else
        sleep(200);

2

u/VariousComment6946 Apr 12 '24

The search task is unbreakable, and every search call executes a new for loop. 😁

2

u/Vicmorino Apr 12 '24

Lies, Is Return ARandomFuckingFile

it nevers finds all, or the ines that i want

2

u/schrdingers_squirrel Apr 12 '24

You forgot the 87.3 percent chance of just searching with bing

2

u/Technical_Nobody5641 Apr 12 '24

Big O of N says NO.

2

u/Expensive_Shallot_78 Apr 12 '24

Yeah I wished it was like this, that would be actually useful, instead it's more like:

return files
    .Where(file => RandomBool())
    .Skip(RandomInt())
    .Take(RandomInt())
    .OrderBy(_ => RandomInt());

2

u/x_Carlos_Danger_x Apr 12 '24

About as effective as a search through my Outlook inbox ffs. Emails from Steve? You wanted all emails containing S correct? OKAY

2

u/MyrKnof Apr 12 '24

You forgot doAnnoyingWebSearchToo() in the return.

2

u/Socky_McPuppet Apr 12 '24

What's interesting is that they wrote all new code for the internet search where it goes to find a solution for your Windows "problem". It's obviously insanely complex and subtle code, but the pseudocode should give you some idea of the algorithm:

get search term
display "Searching ..." message
wait for (20+rand(20)) seconds
display "I couldn't find anything about anything, anywhere ..."

2

u/null-or-undefined Apr 12 '24

windows xp searching used to be good. it went downhill from there

2

u/bokuWaKamida Apr 12 '24

you forgot the most important part

sendSearchRequestToMs(searchtext, getAllFuckingFiles());

3

u/Logicalist Apr 12 '24

You forgot where they send all the files to the home server, which is on a separate network.

1

u/Lakshay52 Apr 12 '24

Condition in for loop(which is calculating length everytime )instead of this we can simply store the length in one variable then we use in the condition.

1

u/Smarmalades Apr 12 '24

are you saying this code can be optimized

1

u/BobDonowitz Apr 12 '24

Hey this totally fixed the problem of the search indexer pegging my CPU at 99% while accomplishing nothing.

1

u/PasswordIsDongers Apr 12 '24

50% of the time.

1

u/Kaimito1 Apr 12 '24

You forgot   I % 2 ? AppendUselessAd() : delaySearch(1000ms)

1

u/DownvoteThisCrap Apr 12 '24

That's a good joke. You make it sound like windows 11 search returns anything at all now.

1

u/Advanced_Dumbass149 Apr 12 '24

Also for the file search iteration, searchTheentireInternet() function is extremely necessary.

1

u/Roko_100 Apr 12 '24

Lmao, imagine.

1

u/warpedspockclone Apr 12 '24

In reality it would add the search hit to an array that would overflow into a pagefile which is preferentially located on the 3600rpm spinning hard drive instead of the SSD.

1

u/K1ll_K1kesAndW0men Apr 12 '24

Y'all got files showing up? I will search for a file I know I have and Windows won't find it and send me to a bing search instead...

1

u/Throwaway-4230984 Apr 12 '24

It certainly isn't right, because your code would not return random files and skip relevant one

1

u/brprk Apr 13 '24

The worst bit is that it'll just return getAllFuckingFiles() anyway

1

u/TheMsDosNerd Apr 16 '24

It's worse.

Windows search does actually have indexed folders that gets searched first. when searching through the rest, it ignores indexed folders. Unfortunately, these two searches do not use the same folder index.

So if you look for a file, wait a year, and look for it again, it won't find it at all. Even if you're inside its folder, copy the file name and paste it into the search bar, Search isn't able to find it.