Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Post
  • Reply
Bozart
Oct 28, 2006

Give me the finger.

LockeNess Monster posted:

Well in FASTA format N is supposed to mean that sequencer wasn't sure what nucleotide it was, so N is actually legit in input data. However also notice passing string by value, not consting it as well as method not being const and returning a brand new string.

My point is that in any sane system, the uppercase command does not get to decide what is an invalid input from the sequencer or whatever. I sometimes deal with code written like this, and usually how slow the program is running is the last of the worries since you basically have to scrap it all anyway.

Adbot
ADBOT LOVES YOU

jonjonaug
Mar 26, 2010

by Lowtax
Why even bother doing that if all incorrect input is going to be set to "N" when you take it in (at least I'm assuming it should already be doing this).

code:
string WordDistribution::toUppercase( string check )
{
        for( size_t ii = 0; ii < check.length(); ii++ )
        {
                if(check[ii]>'Z')
                    check[ii] -= 0x20;
        }
        return check;
}
E: Yeah pretty much what the guy above me said.

jonjonaug fucked around with this message at 01:54 on Apr 26, 2010

1337JiveTurkey
Feb 17, 2005

Shumagorath posted:

I'm not sure what virtual memory has to do with that, but hasn't Microsoft been trying to bring this back for WinFS for a decade now and they just keep getting held up?

WinFS is more than just single level storage in that it's got searching plus pretty much a full OO database with sharing and permissions and every bell and whistle that could possibly exist. SLS on its own simply treats disk storage as plain old memory and RAM as cache. Multics used both segmented and paged memory, and treated every file as a segment that could be mapped into a process space. File reading and writing was performed simply by reading and writing into that segment and the OS would handle it by mapping the pages to memory for reading and writing dirty pages to disk.

Since modern operating systems already do have mechanisms like mmap, I guess that you could say that we do have SLS of a sort as an option although the flat memory space makes it difficult to map the whole file.

jandrese
Apr 3, 2007

by Tiny Fistpump
Shoot, if you're doing a million of them and your input is sanitized, you could simply go:

code:
//convert all characters in a word to upper_case
string WordDistribution::toUppercase( string check )
{
        for( size_t ii = 0; ii < check.length(); ii++ )
        {
                check[ii] &= ~0x20;    // Convert the character to uppercase
        }
        return check;
}
Saves you a possible branch, which can make your processor pipeline much happier with you. I'd also consider caching the result of check.length() if this is a critical loop. Your optimizer is smart enough to do it for you (but it might not, and you might be making a function call on every iteration).

jandrese fucked around with this message at 19:06 on Apr 26, 2010

clockwork automaton
May 2, 2007

You've probably never heard of them.

Fun Shoe
code:
void Counter::setRef(View* dv){
	viewRef = dv;
}

Counter& Counter::operator=(int num){
	
	total = num;

	return *this;
}

Counter& Counter::operator+=(int num){
	total += num;
	return *this;
}


Counter& Counter::operator-=(int num){
	total -= num;
	return *this;
}


string Counter::getStringOf(){
	if(total == 1){
		return ofToString(total) + " thing";
		
	}
	return ofToString(total) + " things";
	
} 
No, this is not some introduction to C++ and operation overloading code - this is production code. :(

POKEMAN SAM
Jul 8, 2004

clockwork automaton posted:

code:
string Counter::getStringOf(){
	if(total == 1){
		return ofToString(total) + " thing";
		
	}
	return ofToString(total) + " things";
	
} 

This one is my favorite, haha.

Scaramouche
Mar 26, 2001

SPACE FACE! SPACE FACE!

Ugg boots posted:

This one is my favorite, haha.

I don't have the code in front of me but a friend was doing some RPG style coding and basically made a giant exception list for items that don't pluralize generically. E.g. "pair of pantses" that looked like the above. Not many elegant ways to do it though; maybe identify the major sub-cases and use some kind of opcode to control pluralization behavior. (Already plural, ends in s, default plural, etc.)

Dessert Rose
May 17, 2004

awoken in control of a lucid deep dream...

Scaramouche posted:

I don't have the code in front of me but a friend was doing some RPG style coding and basically made a giant exception list for items that don't pluralize generically. E.g. "pair of pantses" that looked like the above. Not many elegant ways to do it though; maybe identify the major sub-cases and use some kind of opcode to control pluralization behavior. (Already plural, ends in s, default plural, etc.)

You can store the pluralized name as a field with the item object and set it when creating the items; your tools can have a much dumber pluralization routine to set the default.

Smugdog Millionaire
Sep 14, 2002

8) Blame Icefrog
code:
public override void Error(string message)
{
    throw new NotImplementedException();
}
That's an Error all right. From a review of work done by "professional services."

No Safe Word
Feb 26, 2005

Free Bees posted:

code:
public override void Error(string message)
{
    throw new NotImplementedException();
}
That's an Error all right. From a review of work done by "professional services."

Right-click "create method stub for .." is exactly what happened there.

HFX
Nov 29, 2004

jandrese posted:

Shoot, if you're doing a million of them and your input is sanitized, you could simply go:

code:
//convert all characters in a word to upper_case
string WordDistribution::toUppercase( string check )
{
        for( size_t ii = 0; ii < check.length(); ii++ )
        {
                check[ii] &= ~0x20;    // Convert the character to uppercase
        }
        return check;
}
Saves you a possible branch, which can make your processor pipeline much happier with you. I'd also consider caching the result of check.length() if this is a critical loop. Your optimizer is smart enough to do it for you (but it might not, and you might be making a function call on every iteration).

I would write it as check.length and then if it was really eating a lot of time in the loop see if changing it would help. You are correct on the if statement if and only if you know the input data is correct and does not effect results later.

jonjonaug
Mar 26, 2010

by Lowtax

HFX posted:

I would write it as check.length and then if it was really eating a lot of time in the loop see if changing it would help. You are correct on the if statement if and only if you know the input data is correct and does not effect results later.

Better yet:

code:
//convert all characters in a word to upper_case
string WordDistribution::toUppercase( string check )
{
        for( size_t ii = 0; check[ii] != '\0'; ii++ )
        {
                check[ii] &= ~0x20;    // Convert the character to uppercase
        }
        return check;
}
Or if you don't trust your compiler to optimize it out:

code:
//convert all characters in a word to upper_case
string WordDistribution::toUppercase( string check )
{
        size_t temp=check.length();
        for( size_t ii = 0; ii < temp; ii++ )
        {
                check[ii] &= ~0x20;    // Convert the character to uppercase
        }
        return check;
}

Mustach
Mar 2, 2003

In this long line, there's been some real strange genes. You've got 'em all, with some extras thrown in.
std::strings can contain \0. You should be using iterators instead of random access if you're savagely optimizing. Do you have a problem with the standard library function toupper()?

tractor fanatic
Sep 9, 2005

Pillbug

Mustach posted:

std::strings can contain \0. You should be using iterators instead of random access if you're savagely optimizing. Do you have a problem with the standard library function toupper()?

Iterators are faster than random access for std::string?

blorpy
Jan 5, 2005

jonjonaug posted:

Better yet:

code:
//convert all characters in a word to upper_case
string WordDistribution::toUppercase( string check )
{
        for( size_t ii = 0; check[ii] != '\0'; ii++ )
        {
                check[ii] &= ~0x20;    // Convert the character to uppercase
        }
        return check;
}
Or if you don't trust your compiler to optimize it out:

code:
//convert all characters in a word to upper_case
string WordDistribution::toUppercase( string check )
{
        size_t temp=check.length();
        for( size_t ii = 0; ii < temp; ii++ )
        {
                check[ii] &= ~0x20;    // Convert the character to uppercase
        }
        return check;
}

You guys are missing the obvious optimizations your video card could be doing. This sort of parallel work is perfect for GPGPU/CUDA/etc. Just think how much faster this could run in such an environment!

Mustach
Mar 2, 2003

In this long line, there's been some real strange genes. You've got 'em all, with some extras thrown in.

tractor fanatic posted:

Iterators are faster than random access for std::string?
Probably, if you're sequentially iterating through the characters. Of course, only a profiler can say for sure, but I don't see why I should assume that the counter-intuitive option is worse, especially when most implementations that I've looked at just typedef char*.

Regardless, that kind of optimization is dwarfed by the function looping through the string at least twice: Once to copy the string and another to modify it. And maybe another time, depending on if the compiler can elide the return copy.

blorpy
Jan 5, 2005

Student code is disallowed in this thread, right? My group code-an-OS project has been the source of many horrors.

Zhentar
Sep 28, 2003

Brilliant Master Genius

jandrese posted:

I'd also consider caching the result of check.length() if this is a critical loop. Your optimizer is smart enough to do it for you (but it might not, and you might be making a function call on every iteration).

.length() will almost certainly be inlined, so you're not going to have much overhead even if it isn't cached.

jandrese
Apr 3, 2007

by Tiny Fistpump

Mustach posted:

Probably, if you're sequentially iterating through the characters. Of course, only a profiler can say for sure, but I don't see why I should assume that the counter-intuitive option is worse, especially when most implementations that I've looked at just typedef char*.

Regardless, that kind of optimization is dwarfed by the function looping through the string at least twice: Once to copy the string and another to modify it. And maybe another time, depending on if the compiler can elide the return copy.

If String is actually just typedef char*, then wouldn't the function pass by reference and avoid making the copy?

Although if it is just a typedef, then string.length() could be an O(n) operation, which would make the whole function O(n^2) unless you went and cached the result. Then again, if \0 is a valid character for String classes, then it can't be a simple wrapper for a C style string, the length has to be stored somewhere.

Zhentar
Sep 28, 2003

Brilliant Master Genius

jandrese posted:

If String is actually just typedef char*, then wouldn't the function pass by reference and avoid making the copy?

Although if it is just a typedef, then string.length() could be an O(n) operation, which would make the whole function O(n^2) unless you went and cached the result. Then again, if \0 is a valid character for String classes, then it can't be a simple wrapper for a C style string, the length has to be stored somewhere.

MSVC strings look something like this:

code:
struct string {
	uint allocator; \\This is just garbage
	union {
		char* string;
		char buffer[16];
	}
	uint length;
	uint allocated;
}
GCC skips the buffer.

Mustach
Mar 2, 2003

In this long line, there's been some real strange genes. You've got 'em all, with some extras thrown in.

jandrese posted:

If String is actually just typedef char*
I was referring to std::string::iterator. edit: Don't dwell too much on that specific comment, I'm wrong, at least for GNU's library.

Mustach fucked around with this message at 22:26 on Apr 27, 2010

Painless
Jan 9, 2005

Turn ons: frogs, small mammals, piles of compost
Turn offs: large birds, pitchforks
See you at the beach!
First rule of optimization: assume that your compiler was written in 1983 by an army of white collared chimps with typewriters
Second rule of optimization (follows from first): there's a massive difference between i++ and ++i, especially for primitives
Third rule of optimization: god the neck of this bottle feels amazing in my rear end

Blotto Skorzany
Nov 7, 2008

He's a PSoC, loose and runnin'
came the whisper from each lip
And he's here to do some business with
the bad ADC on his chip
bad ADC on his chiiiiip

Painless posted:

First rule of optimization: assume that your compiler was written in 1983 by an army of white collared chimps with typewriters
Second rule of optimization (follows from first): there's a massive difference between i++ and ++i, especially for primitives
Third rule of optimization: god the neck of this bottle feels amazing in my rear end

:2bong:

click

Mustach
Mar 2, 2003

In this long line, there's been some real strange genes. You've got 'em all, with some extras thrown in.

Painless posted:

Third rule of optimization: god the neck of this bottle feels amazing in my rear end
Gives "bit-twiddling" a whole new meaning

Avenging Dentist
Oct 1, 2005

oh my god is that a circular saw that does not go in my mouth aaaaagh
code:
<AvengingDentist> !butt bit-twiddling
<buttebot> AvengingDentist: butt-twiddling

1337JiveTurkey
Feb 17, 2005

Amusing anecdotes about systems programming on the VAX 11/780 are truly the fons et origo of all best programming practices.

king_kilr
May 25, 2007

Painless posted:

First rule of optimization: assume that your compiler was written in 1983 by an army of white collared chimps with typewriters
Second rule of optimization (follows from first): there's a massive difference between i++ and ++i, especially for primitives
Third rule of optimization: god the neck of this bottle feels amazing in my rear end

Well those are good, but I prefer mine

First rule is READ THE loving ASSEMBLY. Second rule is PROFILE YOUR loving poo poo. The third rule is interchange 1 and 2 as approrpiate.

blorpy
Jan 5, 2005

Ok, this stuff is from two of my teammates in my OS project. It's student code so you'd expect some level of shittiness but then again, it is a junior-level course. Also, if you don't know C you have no business calling yourself a computer engineer. :colbert:

On with the lovely code.

Guess what this is doing (variable names changed slightly and extraneous code removed to highlight the horror)

code:
int32_t syscall_open(const uint8_t *filename)
{
    ...
    for(int i = 0; i < 63; i++)
    {
        if(thing[i].file_name == (char)filename)
        {
            ...
        }
    }
}

(thing[i].file_name is a char array)
String comparison! Because that's totally how string compare in C works.

The next horror isn't about code itself. I had a continue; in my code somewhere and these two brilliant guys asked me where this "continue function" was declared. Think about that for a second. They thought that I was calling a function named continue, despite not having any parens and despire continue being highlighted by their text editors in the same color as every other reserved word.

Finally, l'horreur finale with a little bit of code I can demonstrate.

During a rush to get one our checkpoints done I wrote some somewhat hackish code. The same two teammates as before decided to "refactor" afterwards in an attempt to scrub off the hackishness. In particular, there was an issue where the assignment sheet happened to mention storing an inode_t * inside a file descriptor struct. I stored the inode_t itself because we have no memory allocation and there's no actual points based on storing a inode_t * instead of the inode_t itself. I figured that would be the last I'd ever hear of it but then I take a look at the code and see that they've gone through and changed all the inode_ts to inode_t *s without actually allocating any space! Worse yet, they changed
code:
inode_t node;
get_inode_by_name(name, &node);
to
code:
inode_t *node;
get_inode_by_name(name, node);
which clearly does not even initialize the pointer! asjhaosjaoijs ffffffffffffffff

The worst thing is that because we're in kernel mode this "works" most of the time for small tests.

When I pointed this out, I saw the following change shortly afterwards.
code:
inode_t *node = NULL;
get_inode_by_name(name, node);
:suicide:

tl;dr: 2 of my teammates manage to do negative work on this project. sadly i feel they're probably going to graduate with the same degree as me.

theg sprank
Feb 3, 2008
pillage your village
Really that's all your fault though. You should have picked better partners.

BattleMaster
Aug 14, 2000

theg sprank posted:

Really that's all your fault though. You should have picked better partners.

The only good partner in university is no one at all.

blorpy
Jan 5, 2005

theg sprank posted:

Really that's all your fault though. You should have picked better partners.

From what I can tell these guys are roughly on average skill level for the course.

1337JiveTurkey
Feb 17, 2005

BattleMaster posted:

The only good partner in university is no one at all.

I once had a professor who told me that I had to do one project individually since group projects amounted to having three people watch me do all the work. :smuggo:

shrughes
Oct 11, 2008

(call/cc call/cc)
When I had a group project, I got stuck with the three other kids who were not turned on by all the other groups' stupid ideas. We ended up being the awesomest group. The end.

POKEMAN SAM
Jul 8, 2004

1337JiveTurkey posted:

I once had a professor who told me that I had to do one project individually since group projects amounted to having three people watch me do all the work. :smuggo:

I always felt like such a retard for asking if I could do group projects by myself, but I sure as hell didn't want to explain, for example, in a Math class why I was writing a C# application to generate the linear equations for the linear programming problem. All of the other groups wrote out all of the combinations by hand (this was a scheduling problem.)

cowtown
Jul 4, 2007

the cow's a friend to me

Janin posted:

Please tell me it involves genetics in some way

code:
int Are_Compatible(char *statea, char *stateb, int stepsize, int datatype)
{
	int i,j;
	char a,b;


	if(datatype == NT)
	{
		For(i,stepsize)
		{
			a = statea[i];
			For(j,stepsize)
			{
				b = stateb[j];

				switch(a)
				{
				case 'A':
				{
					switch(b)
					{
					case 'A' :
					case 'M' :
					case 'R' :
					case 'W' :
					case 'D' :
					case 'H' :
					case 'V' :
					case 'X' : {b=b; break;}
					default : return 0;
					}
					break;
				}
				case 'G':
				{
					switch(b)
					{
					case 'G' :
					case 'R' :
					case 'S' :
					case 'K' :
					case 'B' :
					case 'D' :
					case 'V' :
					case 'X' : {b=b; break;}
					default : return 0;
					}
					break;
				}
...
and so on for 586 lines.

ymgve
Jan 2, 2004


:dukedog:
Offensive Clock
Not sure if it qualifies, but what do you do when your programming language doesn't support bitwise operators?

You create them from scratch. (search for "poor man's bitlib")

I am particularly fond of their bitwise AND implementation:
code:
function band (a,b) return ((a+b) - bxor(a,b))/2 end

GROVER CURES HOUSE
Aug 26, 2007

Go on...

cowtown posted:

code:
int Are_Compatible(char *statea, char *stateb, int stepsize, int datatype)
{
	int i,j;
	char a,b;


	if(datatype == NT)
	{
		For(i,stepsize)
		{
			a = statea[i];
			For(j,stepsize)
			{
				b = stateb[j];

				switch(a)
				{
				case 'A':
				{
					switch(b)
					{
					case 'A' :
					case 'M' :
					case 'R' :
					case 'W' :
					case 'D' :
					case 'H' :
					case 'V' :
					case 'X' : {b=b; break;}
					default : return 0;
					}
					break;
				}
				case 'G':
				{
					switch(b)
					{
					case 'G' :
					case 'R' :
					case 'S' :
					case 'K' :
					case 'B' :
					case 'D' :
					case 'V' :
					case 'X' : {b=b; break;}
					default : return 0;
					}
					break;
				}
...
and so on for 586 lines.

Find whoever wrote that and harvest their kidneys.

Janitor Prime
Jan 22, 2004

PC LOAD LETTER

What da fuck does that mean

Fun Shoe

1337JiveTurkey posted:

I once had a professor who told me that I had to do one project individually since group projects amounted to having three people watch me do all the work. :smuggo:

I once told a kit in front of the whole class that he couldn't be my partner because he sucked too much. :smugdog:

tractor fanatic
Sep 9, 2005

Pillbug
http://everything2.com/title/C%252B%252B%253A+Checking+units+at+compile+time

I love this

Adbot
ADBOT LOVES YOU

raminasi
Jan 25, 2005

a last drink with no ice
Then you'll love Boost MPL

  • 1
  • 2
  • 3
  • 4
  • 5
  • Post
  • Reply