问:
Data Oriented Design - impractical with more than 1-2 structure "members"?
The usual example of Data Oriented Design is with the Ball structure:

```<code>struct Ball
{
  float Radius;
  float XYZ[3];
};
</code>```

and then they make some algorithm that iterates a std::vector<Ball> vector.

Then they give you the same thing, but implemented in Data Oriented Design:

```<code>struct Balls
{
  std::vector<float> Radiuses;
  std::vector<XYZ[3]> XYZs;
};
</code>```

Which is good and all if you're going to iterate trough all radiuses first, then all positions and so on. However, how do you move the balls in the vector? In the original version, if you have a std::vector<Ball> BallsAll, you can just move any BallsAll[x] to any BallsAll[y].

However to do that for the Data Oriented version, you must do the same thing for every property (2 times in the case of Ball - radius and position). But it gets worse if you have a lot more properties. You'll have to keep an index for each "ball" and when you try to move it around, you have to do the move in every vector of properties.

Doesn't that kill any performance benefit of Data Oriented Design?

答:
Short answer: you are fully correct, and articles like this one are completely missing this point.

The full answer is: the "Structure-Of-Arrays" approach of your examples can have performance advantages for some kind of operations ("column operations"), and "Arrays-of-Structs" for other kind of operations ("row operations", like the ones you mentioned above). The same principle has influenced database architectures, there are column-oriented databases vs. the classical row oriented databases

So the second thing to consider for choosing a design is what kind of operations you need most in your program, and if those will benefit from the different memory layout. However, the first thing to consider is if you really need that performance (I think in games programming, where the above article is from you often have this requirement).

Most current OO languages use an "Array-Of-Struct" memory layout for objects and classes. Getting the advantages of OO (like creating abstractions for your data, encapsulation and more local scope of basic functions), is typically linked to this kind of memory layout. So as long as you don't do high performance computing, I would not consider SoA as the primary approach.


问:
Coding style - define fields separate or not
Some use this convention:

```<code>private String event;
public getEvent() { return this.event; }
public setEvent(String value) { this.event = value; }

private int id;
... same as above ...
</code>```

In some projects they define all the fields first, all static fields after (or before), all public/private variables, then the setters, getters, other methods.

Which do you use and why?

答: I put all the fields, then constructors, then get/sets if any, then methods. That way you can see all of the "meta" object information at the same time. Most of the time, there won't be anything special to see about the get/sets, so I see no point in cluttering up the variable declarations with them.</p>

ContentLicense: CC BY-SA 2.5


问:
What procedural languages support algebraic data types?
One of the things I like about the new Apple language Swift is that it combines procedural programming with algebraic data types. What other languages do this?</p>

答:
You can go full imperative in ML-family languages (e.g. Ocaml) while enjoying ADTs and pattern matching.

You can have a sort of ADT in Algol descendants like C and Pascal by using variant records. But you will get very little syntactic sugar. You'll have to pack/unpack and check the variants manually, without the static type safety that e.g. Haskell would give you.

ContentLicense: CC BY-SA 3.0


答:
Java/C# don't support ADTs out of the box but with some boilerplate and lambdas you can pull it off.

If I'm not mistaken that's also pretty much what Scala's case classes are, minus the boilerplate.

Title: What procedural languages support algebraic data types?

Body: One of the things I like about the new Apple language Swift is that it combines procedural programming with algebraic data types. What other languages do this?</p>


问:
Is CType() ever the preferred option?
Is there any time CType() is the proper option over other methods? I did plenty of thinking on this myself, but wanted to put the question in hopes there would be answer to my question so I can put the question to rest in my workplace.

My idea is that CType() should never be used.  But others seem to think it's still around for something other than backward-compatibility.  So is there still a use for it?

A big problem is that many people don't seem to understand the difference between Casting and Converting.

Converting Vs Casting

This was one such line of code:

```<code>With objZipFile.GetEnumerator()
    While .MoveNext
        objZipEntry = CType(.Current, ZipEntry)
        … all this work is done with the objZipEntry
    End While
End With
</code>```

If my buddy ever reads this, he'll be like, "MY CODE!!!!" lol.  Yes, this is your code.  And as I discussed with him, .Current is an enumeration of objZipFile, but its type is ZipEntry.  So why is he "Converting" with CType(), when what he's really doing here is Casting.  He should be using DirectCast(.Current, ZipEntry).

So the proper way to Cast something is to use DirectCast().  But this isn't a discussion about DirectCast().  The point here is that CType() Should certainly not be used to Cast, it's a Conversion tool, so let's talk more about Converting.

To CType() or Not To CType(), I don't think so

In the simplistic example, I have a string "123", and I want the string to be converted to an Integer.  So many times, you see something like this:

```<code>Dim result as Integer = 0
result = CType("123", Integer)
</code>```

Great!  It Works!  Then what is the difference between that and this:

```<code>Dim result as Integer = 0
result = Integer.Parse("123")
</code>```

Well, with that exact example, the difference is that CType() took more resources to run.  If I gave you two clear drinks and told you one had a health rating of 10, and the other of 9.  You don't choose the 9 on principle that it isn't that much less healthy, you choose the dam 10.  Also, the Integer.Parse line is a couple characters of less typing.

Okay, but what if your input isn't "123"?  What if the input is "UrMom"?  I hear CType() won't break... but as far as I know, that isn't true.  CType() breaks just as much as Type.Parse().  I'd like some arguments on that.  Though an argument supporting CType()'s resilience alone isn't reason enough to use CType().

On the other hand, we have this option in the Type-Class:

```<code>Dim MyInt As Integer = 0
Dim TryInt As Integer = 0
If Integer.TryParse("54", TryInt) Then
    MyInt = TryInt
Else
    'Handle it appropriately
End If
</code>```

Not only are we parsing the data, but if it doesn't parse, we are handling it correctly.  Which is a much better option that throwing a Try/Catch around bad code and letting it break before handling its failure in the Catch/Finally.

Keep in mind, there may be multiple different ways to cast and convert, but the main purpose of this question is to prove or disprove that CType() simply shouldn't be used.

My main argument is this... The only arguments I've seen that support the use of CType() are those that say CType() won't break if passed bad data, or that CType() is better if you are unsure of the data going in.  In response to that I say Pish-Posh.  Rethink your code, place conditionals, and know what you're wanting to use the item for.  If something happens that you weren't expecting, handle it correctly.

So is there any time CType() is the proper option over other methods? Please provide examples.

答:
Edit: This question/answer pair were during a C++/C# developer's short stint in the confusing world of Visual Basic.  It turns out that the proper answer is: Don't use Visual Basic. At this time, the best support for CType() is a VB6 to early .NET support document from 2003. EndEdit

After allowing this question to live online for a while, and also working with peers on research and usage regarding Casting and Converting, the answer has become clear.

The question was: Is there any time CType() is the proper option over other methods?

The answer is: No.  From this point on, never use CType().

The only argument is: "What if you don't know the type you're casting?"

The counter argment to that is: Then refactor your program, be more explicit with your intentions, and perhaps stop using weak-typed objects, "strong-typed" datasets, or datasets in general.

Title: Is CType() ever the preferred option?


问:
How can I use unit tests and TDD to test an app that relies mostly on database CRUD operations?
At work, one of my projects is mostly about taking data passed in from an external client and persisting it in a database.  It's a Java enterprise app using JPA and most of our logic revolves around CRUD operations.

The majority of our bugs involve JPA in one way or another.

Example 1: If you click the save button twice, JPA might try to insert the same entity into the database a second time, causing a primary key violation.

Example 2: You retrieve an entity from the database, edit it and try to update its data.  JPA may try to create a new instance instead of updating the old one.

Often the solution is needing to add/remove/change a JPA annotation.  Other times it has to do with modifying the DAO logic.

I can't figure out how to get confidence in our code using unit tests and TDD. I'm not sure if it's because unit tests and TDD are a bad fit, or if I'm approaching the problem wrong.

Unit tests seem like a bad fit because I can only discover these problems at runtime and I need to deploy to an app server to reproduce the issues.  Usually the database needs to be involved which I consider to be outside the definition of a unit test: These are integration tests.

TDD seems like a bad fit because the deploy + test feedback loop is so slow it makes me very unproductive.  The deploy + test feedback loop takes over 3 minutes, and that's just if I run the tests specifically about the code I'm writing.  To run all the integration tests takes 30+ minutes.

There is code outside this mold and I always unit test that whenever I can.  But the majority of our bugs and the biggest time sinks always involve JPA or the database.

There is another question that is similar, but if I followed the advice I'd be wrapping the most unstable part of my code (the JPA) and testing everything but it. In the context of my question, I'd be in the same bad situation. What's the next step after wrapping the JPA? IMO that question is (perhaps) a step to answer my question, but not an answer to it.

答:
One option is to use an in-memory testing database such as H2; it tends to be about about 10x faster than a standard disk-using database, and with lower startup/teardown times.

Whether it will help does largely depend on whether the JPA issues you are having are general enough that they will still fail on different database. Not much point running tests faster if they miss the bulk of the problems.

But if you can do 10 runs with H2 for every one with the full system, it could pay off.


答:
Databases can be very easy to unit test - you need stored procedures and transactions.

This what Microsoft says about Database unit testing. You can also run unit tests against a database, writing your tests in Java or C# by setting up a DB connection, beginning a transaction, write whatever data you want to use for the test to the DB, run the tests and then rollback. No damage to the DB if you were using one you also deployed to and you get fully isolated tests.

Hope this can give you some insight how to do it within your framework.

Title: How can I use unit tests and TDD to test an app that relies mostly on database CRUD operations?

Body:
At work, one of my projects is mostly about taking data passed in from an external client and persisting it in a database.  It's a Java enterprise app using JPA and most of our logic revolves around CRUD operations.

The majority of our bugs involve JPA in one way or another.

Example 1: If you click the save button twice, JPA might try to insert the same entity into the database a second time, causing a primary key violation.

Example 2: You retrieve an entity from the database, edit it and try to update its data.  JPA may try to create a new instance instead of updating the old one.

Often the solution is needing to add/remove/change a JPA annotation.  Other times it has to do with modifying the DAO logic.

I can't figure out how to get confidence in our code using unit tests and TDD. I'm not sure if it's because unit tests and TDD are a bad fit, or if I'm approaching the problem wrong.

Unit tests seem like a bad fit because I can only discover these problems at runtime and I need to deploy to an app server to reproduce the issues.  Usually the database needs to be involved which I consider to be outside the definition of a unit test: These are integration tests.

TDD seems like a bad fit because the deploy + test feedback loop is so slow it makes me very unproductive.  The deploy + test feedback loop takes over 3 minutes, and that's just if I run the tests specifically about the code I'm writing.  To run all the integration tests takes 30+ minutes.

There is code outside this mold and I always unit test that whenever I can.  But the majority of our bugs and the biggest time sinks always involve JPA or the database.

There is another question that is similar, but if I followed the advice I'd be wrapping the most unstable part of my code (the JPA) and testing everything but it. In the context of my question, I'd be in the same bad situation. What's the next step after wrapping the JPA? IMO that question is (perhaps) a step to answer my question, but not an answer to it.


问:
Going through The C Programming Language K&R in Visual Studio
From what I have read, K&R seems to be a good place to start learning programming in general, and C programming specifically. However, I've just started the first chapter and I have a few questions. They may be extremely simple, and I apologize if that's the case (I am new, after all) -- but your helping me answer them would be very appreciated.

1) I understand the book is written to describe C languages in general. Does this mean the examples they give are just rough outlines, and that for me to attempt them myself I have to "translate" the general outline given into C++ or C# or whatever?

For example, the textbook gives this program:

```<code>#include <stdio.h>
 main()
 {
 printf("hello, world\n");
 }
</code>```

But I've found a program to print "Hello, world" in C#, for example is:

```<code>// A Hello World! program in C#.
using System;
namespace HelloWorld
{
    class Hello
    {
        static void Main()
        {
            Console.WriteLine("Hello World!");

            // Keep the console window open in debug mode.
            Console.WriteLine("Press any key to exit.");
            Console.ReadKey();
        }
    }
}
</code>```

And the program in C++ looks different from both the above...

So my question is: does the book expect me to be able to translate the first program (or outline, whatever it is) to a C# or C++ program -- I am using Visual Studio -- to be able to practice myself?

2) If the book does expect me to do this, do you all have any tips on how I can go about translating general C code to C# or C++.

3) This may be opinion, but which should I base my learning on (and practice coding in) -- C# or C++?

答:
The K&R book is a great place to start with any of the C variants.

The short answer is that you can run C code through a C++ compiler and it will run.

Some things you need to know:

When C++ was introduced, it updated C's capabilities by (at a minimum) adding object oriented coding to C. (UPDATE) As many of you have noted, the languages are not the same, continuing to grow further apart with time. (/UPDATE). According to the wikipedia article, Compatibility of C and C++, there are semantic differences:

The C and C++ programming languages are closely related. C++ grew out of C, as it was designed to be source-and-link compatible with C.1 Due to this, development tools for the two languages (such as IDEs and compilers) are often integrated into a single product, with the programmer able to specify C or C++ as their source language. However, due to minor semantic differences, most non-trivial C programs will not compile as C++ code without modification — C++ is not a superset of C. Emphasis added.

C++ compilers (specifically VS) typically will run C code with minor or no modifications depending on the complexity of your program. You may have to set various compiler/linker options. This will depend on which compiler and version you are using. Take a look at Compiling a C/C++ Program. And What issues can I expect compiling C code with a C++ compiler?. Also check out Running C in a C++ compiler.

C# is a newer language that has distanced itself from its C roots, so you will have to translate C to C# to get it to work. What are the differences between C, C# and C++ in terms of real-world application.

Both C and C++ give you a lower level of abstraction that, with increased complexity, provides a breadth of access to underlying machine functionality that are not necessarily exposed with other languages. C++ adds the convenience (reduced development time) of a fully object oriented language which can, potentially, add an additional performance cost.

C# provides a managed memory model that adds a higher level of abstraction again. This level of abstraction adds convenience and improves development times, but complicates access to lower level APIs and makes specialized performance requirements problematic.


答:
I understand the book is written to describe C languages in general.

The book is written to teach you ANSI C, not 'C languages in general'

Does the book expect me to be able to translate the first program ... to a C# or C++ program?

No, those are different languages, and the book does not expect you to do anything with those languages. The examples are ANSI C examples.

Do you all have any tips on how I can go about ...

Which should I base my learning on (and practice coding in) -- C# or C++?

Those questions, while perfectly fine, are off-topic for this site.

Suffice it to say, if you want to learn C# or C++, you should seek out resources for those specific languages.

Title: Going through The C Programming Language K&R in Visual Studio

Body:
From what I have read, K&R seems to be a good place to start learning programming in general, and C programming specifically. However, I've just started the first chapter and I have a few questions. They may be extremely simple, and I apologize if that's the case (I am new, after all) -- but your helping me answer them would be very appreciated.

1) I understand the book is written to describe C languages in general. Does this mean the examples they give are just rough outlines, and that for me to attempt them myself I have to "translate" the general outline given into C++ or C# or whatever?

For example, the textbook gives this program:

```<code>#include <stdio.h>
 main()
 {
 printf("hello, world\n");
 }
</code>```

But I've found a program to print "Hello, world" in C#, for example is:

```<code>// A Hello World! program in C#.
using System;
namespace HelloWorld
{
    class Hello
    {
        static void Main()
        {
            Console.WriteLine("Hello World!");

            // Keep the console window open in debug mode.
            Console.WriteLine("Press any key to exit.");
            Console.ReadKey();
        }
    }
}
</code>```

And the program in C++ looks different from both the above...

So my question is: does the book expect me to be able to translate the first program (or outline, whatever it is) to a C# or C++ program -- I am using Visual Studio -- to be able to practice myself?

2) If the book does expect me to do this, do you all have any tips on how I can go about translating general C code to C# or C++.

3) This may be opinion, but which should I base my learning on (and practice coding in) -- C# or C++?


问:
extensible effects in purescript
In Purescript, IO an other effects are managed with the Eff monad, and extensible effects, which are declared like this:

```<code>main :: Eff (trace :: Trace, random :: Random) {}
</code>```

So if I understand well, this main declaration only allows to log to console, and generate random numbers. The trace :: Trace, random :: Random is a row of effect, ie an unordered labeled collection of effects.

Now I get that Trace is an effect, and trace is its label. But why do we need both? It makes sense for records such as { foo::String, bar::String } but I cannot understand why I would need to manage two Random effect with different labels. ie I would write the following:

```<code>main :: Eff (Trace, Random) {} -- no redundant labels
</code>```

Are effect labels useful in purescript? If yes what for?

答:
The simple answer is that the machinery for rows already existed in the typechecker, from the very first release of the PureScript compiler, so it made sense to reuse it to define the Eff monad. The only change was to allow rows to be parameterized by the kind of the types indexed by their labels.

It certainly seems like labels are unnecessary, and they would be if effects were merely symbols. Another answer, however, is that labels allow us to keep the desirable property of having most general unifiers in the type system.

Contrived Example

Suppose you defined a handler whose type contained a row of effects with two or more unknown type variables:

runFooBarEffects :: forall eff foo bar. Eff (foo :: foo, bar :: bar | eff) a -> Eff (combined :: Baz foo bar | eff) a

And a corresponding action with concrete effect types:

myAction :: Eff (foo :: Foo, bar :: Bar, trace :: Trace) String

When we try to run this action with runFooBarEffects myAction, the typechecker correctly infers the result type Eff (combined :: Baz Foo Bar, trace :: Trace) String by unification.

However, imagine if there were no labels present. The unification algorithm would not be able to proceed, because foo could be unified with either Foo or Bar, since rows are unordered. There is no longer a most general unifier for the two rows.

This example is indeed contrived, but illustrates the point that since we are not restricted to simple atomic types (symbols) appearing in rows, but instead have a whole lattice of types, we need labels to keep the type system sane.

Title: extensible effects in purescript


答:
ML family: OCaml, SML, F#

ML was (AFAIK) the first language to directly support algebraic data types; ML and its descendants have always at least had mutable state through reference variables.

Racket

Within the Racket family there is #lang plai which directly provides algebraic types.

#lang racket supports the same style of program using multiple subtypes of a struct type and using the match macro. There's a good talk on the relationship between algebraic types and this design in this talk, part of the Brown online PL course.

Typed Racket provides explicit union types and matching through the match macro.

Scala

Scala's case classes are algebraic types built on top of the JVM's class system.

Others

Wikipedia has a list of languages with algebraic data types; languages on this list with mutability include Nemerle and haXe.

I've conflated match style operations with algebraic datatypes here, partly because convenience is really all that separates some of these languages from assembly; you can define ad-hoc algebraic types in any language you like, it just won't be very nice.


问:
Store Arbitrary values related to mysql record
I have created an app that manages a Data cleansing workflow. Each row imported needs to store an arbitrary number of key value pairs in addition to our standard mysql schema. The key values need to be searchable. I am using rails + mysql currently. What are my options? As of now,  I am just adding a new fields to the rails schema as new requirements arise, I need a way to store extra data for each row that might not apply to any other rows.</p>

答:
Based on the information you have provided, that seems like a new table (KeyValues table that has three columns: foreign key, key, value where the foreign key links this new table to the existing table.

This will allow the following:

An arbitrary number of key/value pairs can be stored, which can be indexed and searched and you can join on queries with the records in your existing table(s) if needed.

If a row in your existing table(s) does not have any key/value pairs to be stored, then it just wouldn't have entry in this new KeyValues table.

This does not impose any restriction on how many keys every row has and would not require a change in the existing table(s)' schema every time a new key has to be added.

Based on the information in the question, I think if you did a 3NF normalization, you would most likely end up with this kind of a schema.


问:
Reduce a list of IPv4 to lowest common CIDR
I have a long list of IP addresses that show some pattern of closeness

Example:

```<code>XX.249.91.16
XX.249.91.21
XX.249.91.32
XX.249.91.160
XX.249.91.165
XX.249.92.15
XX.249.92.25
XX.249.92.51
XX.249.92.234
</code>```

and sometimes a whois reveals a range like this

```<code>XX.163.224.0 - XX.163.239.255
</code>```

I would like to create a list of CIDR that gives me the lowest common number

It seems Python already has such a thing - iprange_to_cidrs - but I need it in JavaScript

```<code>XX.249.90.0/16
</code>```

if that is what would handle

```<code>XX.249.91.0 - XX.249.92.255
</code>```

ditto  XX.163.200.0/nn to handle

```<code>XX.163.224.0 - XX.163.239.255
</code>```

A calculator that can do it one range at a time when I fiddle with the mask is here
http://www.subnet-calculator.com/cidr.php

My preferred language is JavaScript and I have the following start but need the algorithm that does the match and transform

```<code>var list = [
    "10.249.91.16",
    "10.249.91.21",
    "10.249.91.32",
    "10.249.91.160",
    "10.249.91.165",
    "10.249.92.15",
    "10.249.92.25",
    "10.249.92.51",
    "10.249.92.234"
]
function dec2bin(ip) {
    return ip.map(function(num) {
      return ("00000000"+parseInt(num,10).toString(2)).slice(-8);
    }).join(".");
}
const result = list.map(ip => dec2bin(ip));
</code>```

UPDATE after reviewing an answer

FIDDLE

```<code>const getDec = ip => {
  let octet = ip.split(".");
  return (octet[0] << 24) | (octet[1] << 16) | (octet[2] << 8) | octet[3];
};

var adr1 = "111.163.224.0";
var adr2 = "111.163.239.255"
var XOR = getDec(adr1)^getDec(adr2);

// now what?
</code>```

Update 2:

could the result of the range 111.163.224.0 - 111.163.239.255 be 111.163.224.0/20 ? since we have

```<code>01101111.10100011.11100000.00000000 - 0,111.163.224.0
01101111.10100011.11101111.11111111 - 0,111.163.239.255
</code>```

答:
What you're looking for is a the set of contiguous high-order bits in the address that don't change across the entire set of addresses.  IPv4 addresses are, at their core, 32-bit unsigned integers.  The dotted-quad notation is a convenience for humans.  It's best to deal with them in that form.  Conversion is easy:

```<code>integer_ip = (octet1 << 24) | (octet2 << 16) | (octet3 << 8) | octet4
</code>```

Once you have integers, the right tool for determining which ones change is bitwise exclusive or (^ in many C-derived languages).  This is because it detects change, returning a 1 bit if the corresponding bit in the two values differ, and a 0 otherwise.

I'm going to use 8-bit quantities in the examples to keep it readable, but this applies just the same to the 32-bit numbers you'd get from an IP address.

We have to begin with an integer whose bits indicate which bits changed, which is none of them:

```<code>changed_bits = 0x00
</code>```

For each address after the first one, XOR items n and n-1, resulting in the set of bits that changed between the two.  Those can the be ORed with our running set of changed bits to add to the set:

```<code>a[0] = 0x10
a[1] = 0x11    0x10 ^ 0x11 = 0x01    changed_bits = 0x00 | 0x01 = 0x01
a[2] = 0x1f    0x11 ^ 0x1f = 0x0e    changed bits = 0x01 | 0x0e = 0x0f
</code>```

The end value of changed_bits, 0x0f, indicates that during all comparisons, the upper four bits didn't change (they're all zeros) and the lower four bits did (all ones).  Then all you have to do is count the number of unchanged (zero) bits, starting from the most-significant:

```<code>cidr_size = 0;
for ( bit=0 ; bits < 8 ; ++bits ) {   // Use 32 for IPs
    if ( changed_bits & 0x80 ) {      // Use 0x800000 for IPs
        break;  // Stop when we hit the first bit that changed.
    }
    ++cidr_size;
    changed_bits = changed_bits << 1;  // Rotate the next bit into the high spot
}
</code>```

At this point, cidr_size will hold the number of bits your addresses had in common.  Then you can use that to construct a mask with the proper number of bits

```<code>mask = 0xff << (8 - cidr_size)  // Use 0xffffffff and 32 for IPs
</code>```

With that in hand, you can use it against any of the integers in your list to come up with the CIDR block:

```<code>cidr_block = a[0] & mask
</code>```

Convert cidr_block back to octets, add a slash, spit out cidr_size and you're in business.

Addendum:

What's in this answer will get you the lowest common CIDR for an entire list of addresses.

Given just IPs, there's no magic algorithm to determine the CIDR from which they came.  Based on what they have in common, the IPs used in the question could be included in any of 13 CIDR blocks as large as 10.0.0.0/8 or as small as 10.249.88.0/20.  (Blocks larger than /8 are also possible, but the practical reality is that no block larger than that has ever been issued.)

To properly group the IPs, you would need a database of the CIDRs that have been issued which could be joined with the IPs you have, grouped by CIDR and aggregated to get a count.  The only other thing you could get away with is knowing the netmask that goes with each IP, which isn't going to happen because the receiving end doesn't see it.

ContentLicense: CC BY-SA 4.0

Body:
What you're looking for is a the set of contiguous high-order bits in the address that don't change across the entire set of addresses.  IPv4 addresses are, at their core, 32-bit unsigned integers.  The dotted-quad notation is a convenience for humans.  It's best to deal with them in that form.  Conversion is easy:

integer_ip = (octet1 << 24) | (octet2 << 16) | (octet3 << 8) | octet4

Once you have integers, the right tool for determining which ones change is bitwise exclusive or (^ in many C-derived languages).  This is because it detects change, returning a 1 bit if the corresponding bit in the two values differ, and a 0 otherwise.

I'm going to use 8-bit quantities in the examples to keep it readable, but this applies just the same to the 32-bit numbers you'd get from an IP address.

We have to begin with an integer whose bits indicate which bits changed, which is none of them:

changed_bits = 0x00

For each address after the first one, XOR items n and n-1, resulting in the set of bits that changed between the two.  Those can the be ORed with our running set of changed bits to add to the set:

a[0] = 0x10
a[1] = 0x11    0x10 ^ 0x11 = 0x01    changed_bits = 0x00 | 0x01 = 0x01
a[2] = 0x1f    0x11 ^ 0x1f = 0x0e    changed bits = 0x01 | 0x0e = 0x0f

The end value of changed_bits, 0x0f, indicates that during all comparisons, the upper four bits didn't change (they're all zeros) and the lower four bits did (all ones).  Then all you have to do is count the number of unchanged (zero) bits, starting from the most-significant:

cidr_size = 0;
for ( bit=0 ; bits < 8 ; ++bits ) {   // Use 32 for IPs
    if ( changed_bits & 0x80 ) {      // Use 0x800000 for IPs
        break;  // Stop when we hit the first bit that changed.
    }
    ++cidr_size;
    changed_bits = changed_bits << 1;  // Rotate the next bit into the high spot
}

At this point, cidr_size will hold the number of bits your addresses had in common.  Then you can use that to construct a mask with the proper number of bits

mask = 0xff << (8 - cidr_size)  // Use 0xffffffff and 32 for IPs

With that in hand, you can use it against any of the integers in your list to come up with the CIDR block:

cidr_block = a[0] & mask

Convert cidr_block back to octets, add a slash, spit out cidr_size and you're in business.

Addendum:

What's in this answer will get you the lowest common CIDR for an entire list of addresses.

Given just IPs, there's no magic algorithm to determine the CIDR from which they came.  Based on what they have in common, the IPs used in the question could be included in any of 13 CIDR blocks as large as 10.0.0.0/8 or as small as 10.249.88.0/20.  (Blocks larger than /8 are also possible, but the practical reality is that no block larger than that has ever been issued.)

To properly group the IPs, you would need a database of the CIDRs that have been issued which could be joined with the IPs you have, grouped by CIDR and aggregated to get a count.  The only other thing you could get away with is knowing the netmask that goes with each IP, which isn't going to happen because the receiving end doesn't see it.


答:
Firstly, let me agree that K&R is a great place to start with the C family. It is a really wonderfully written book. Importantly, it is quite concise.

Be aware that C++ is much more closely related to C that C#, although C# does borrow heavily from both. Java in fact does the same, and indeed C# followed from Java.

You can't learn C# or C++ just using K&R - you need resources for each of those.

You seem to be proposing learning all three of C, C++ and C# simultaneously. Whether this is a good thing depends on you. It will give you a great opportunity to compare and contrast the languages without having built up a prejudice from whichever you learn first.

However, this may prove very challenging. Ask yourself, if you wanted to learn Spanish, Portuguese and Italian, would you learn them at the same time or separately?


问:
Should we call Web API from MVC application in same solution?
I am working on a project in MVC that has mobile application so one thing is clear that we have to use Web API so it can used in mobile application.

After creating API when we started to develop Web site we are confused and had discussion on whether to use API or directly access to the Business object. And we ended up after having opinion form more experienced developer to consume Web API instead of using Business object directly.

I'm having confusion regarding to this solution structure.

1) Why we should use Web API and make HTTP request (which is time consuming) to get or put data  instead of business object directly which is in same solution.

2) After having arguments they said what if client wants to host API and web on different cloud server and apply scaling only on API or may be he want to have different url for accessing API and Web (which is some what logical). So in that case should we call Web API from MVC application in same solution?

3) If we're hosting API and Web on different hosting so it means our Web will use WebClient and have HTTP call on each navigation. Is it right?

4) If we'll business object form both API and Web hosting on different server then if something change in BL will need to update build on both server.

5) Or we should create only one project for API and can add views or html pages to develop Web interface so in that way we can directly call API from ajax.

As per my knowledge #5 is the best solution or API is only for 3rd party access. If we have DB, EF, data layer and business layer in same solution then we should not use API to make HTTP calls and directly access business object.
(correct me if I'm wrong)API is needed when mobile application or desktop or any one want to access application so we can have same repository and data layer.

In my scenario I've to create API as we also have mobile application, and in project API side we called business layer (separate project) and business layer communicate to data access layer (separate project). So my question is if we host our API and web to different servers then calling API which is a HTTP request may take longer rather than using method from business layer as we create the project and we've .dll of business layer. In API controller we just convert out put of our business to json format.

I've searched on internet but didn't get convincing  answer. I've found a blog http://odetocode.com/blogs/scott/archive/2013/07/01/on-the-coexistence-of-asp-net-mvc-and-webapi.aspx discussing same point but again in that blog my question is why we need to consider scenario #3?

Update:
We can have different API project and MVC project and we can call API from web using jvascript or can use MVVM pattern.

答:
Directly accessing your business objects directly (I assume you mean in your controller) will be quicker & easier.

what if client wants to host API and web on different cloud server and apply scaling only
  on API or may be he want to have different url for accessing API and Web (which is somewhat logical)

Then you'll need to host them separately... but that doesn't sound very logical to me, surely you would want to scale both.  Are you sure you need to cater for this requirement? That sounds like overkill. Remember YAGNI - if you don't need it, don't build it. When you need it, build it.

If it was me I'd build the site using the technology that fits the best the site, then when (if) you need a service that other people can call build that separately.


问:
Whats a good structure to save and retrieve locations of images?
I got a java-ee application, where I collect informations about movies. Im my backend I provide data like the name, description, genre and a random uuid.

I also got lots of related files, which are stored on a file server. Including some screenshots, the dvd or bluRay cover and video trailers.

My current approach is:

When saving the files to the fileserver, I retrieve the movies random uuid (which is the primary key btw.). I then rename the files screenshot_[UUID]_1, screenshot_[UUID]_2 ... etc.

Now, there are lots of other ways to handle this, like saving all filenames in a database or creating a dir structure on the fileserver for every uuid and, e.g., return all images in the "[uuid]/screenshots" folder via REST.

I expect about 30k requests a day, so the service has to be pretty performant.

Whats the best way to solve this?

答:
As usual, the answer is "it depends". There's lots of different ways you can do it, each solution will vary depending on your unique situation.

I have found that windows tends to barf at any more than 1000 files per directory, so in the past I have created a folder per 1000 files. But you could split it by starting character (eg alphabetically)... or create a hierarchical tree, or split it across drives, or use SQL Server's FILESTREAM... and then you've done lots of premature optimisation and wasted everyone's time!

Try a few scenarios and throw lots of random files at it and see how it performs.

Or just throw everything in one folder and buy lots of SSD drives :)


答:
Other people have answered with "Mock out your DB!" - but what's the point in mocking out your DB layer if you actually need to test how it interacts with your code?

What you're looking for is integration tests and/or automated UI tests. You mentioned that the problem happens when:

```<code>*If you click the save button twice*
</code>```

The only way to test for this is to write an automated UI test to click on the button twice. Maybe check out Selenium.

You will probably also need a unit testing DB and for your tests point it towards that. A pain to maintain but welcome to TDD in the real world.

Body:
Other people have answered with "Mock out your DB!" - but what's the point in mocking out your DB layer if you actually need to test how it interacts with your code?

What you're looking for is integration tests and/or automated UI tests. You mentioned that the problem happens when:

*If you click the save button twice*

The only way to test for this is to write an automated UI test to click on the button twice. Maybe check out Selenium.

You will probably also need a unit testing DB and for your tests point it towards that. A pain to maintain but welcome to TDD in the real world.


答:
What you have described is an implementation problem. OO design is expressly not concerned with implementations.

You can encapsulate your column-oriented Ball container behind an interface that exposes a row- or column-oriented view. You could implement a Ball object with methods like volume and move, which merely modify the respective values in the underlying column-wise structure. At the same time, your Ball container could expose an interface for efficient column-wise operations. With appropriate templates/types and a clever inlining compiler, you can use these abstractions with zero runtime cost.

How often will you be accessing data column-wise vs. modifying it row-wise? In typical use cases for column storage, the ordering of the rows has no effect. You could define an arbitrary permutation of the rows by adding a separate index column. Changing the ordering would only require swapping values in the index column.

Efficient addition/removal of elements could be achieved with other techniques:

Maintain a bitmap of deleted rows instead of shifting elements. Compact the structure when it gets too sparse.

Group rows into appropriate-sized chunks in a B-Tree-like structure so that insertion or removal in arbitrary positions doesn't require modifying the entire structure.

Client code would see a sequence of Ball objects, a mutable container of Ball objects, a sequence of radii, an Nx3 matrix, etc; it doesn't have to be concerned with the ugly details of those complex (but efficient) structures. That's what the object abstraction buys you.

Title: Data Oriented Design - impractical with more than 1-2 structure "members"?

Body:
The usual example of Data Oriented Design is with the Ball structure:

```<code>struct Ball
{
  float Radius;
  float XYZ[3];
};
</code>```

and then they make some algorithm that iterates a std::vector<Ball> vector.

Then they give you the same thing, but implemented in Data Oriented Design:

```<code>struct Balls
{
  std::vector<float> Radiuses;
  std::vector<XYZ[3]> XYZs;
};
</code>```

Which is good and all if you're going to iterate trough all radiuses first, then all positions and so on. However, how do you move the balls in the vector? In the original version, if you have a std::vector<Ball> BallsAll, you can just move any BallsAll[x] to any BallsAll[y].

However to do that for the Data Oriented version, you must do the same thing for every property (2 times in the case of Ball - radius and position). But it gets worse if you have a lot more properties. You'll have to keep an index for each "ball" and when you try to move it around, you have to do the move in every vector of properties.

Doesn't that kill any performance benefit of Data Oriented Design?


问:
Is template "metaprogramming" in Java a good idea?
There is a source file in a rather large project with several functions that are extremely performance-sensitive (called millions of times per second). In fact, the previous maintainer decided to write 12 copies of a function each differing very slightly, in order to save the time that would be spent checking the conditionals in a single function.

Unfortunately, this means the code is a PITA to maintain. I would like to remove all the duplicate code and write just one template. However, the language, Java, does not support templates, and I'm not sure that generics are suitable for this.

My current plan is to write instead a file that generates the 12 copies of the function (a one-use-only template expander, practically). I would of course provide copious explanation for why the file must be generated programmatically.

My concern is that this would lead to future maintainers' confusion, and perhaps introduce nasty bugs if they forget to regenerate the file after modifying it, or (even worse) if they modify instead the programmatically-generated file. Unfortunately, short of rewriting the whole thing in C++, I see no way to fix this.

Do the benefits of this approach outweigh the disadvantages? Should I instead:

Take the performance hit and use a single, maintainable function.

Add explanations for why the function must be duplicated 12 times, and graciously take the maintenance burden.

Attempt to use generics as templates (they probably don't work that way).

Yell at the old maintainer for making code so performance-dependent on a single function.

Other method to maintain performance and maintainability?

P.S. Due to the poor design of the project, profiling the function is rather tricky... however, the former maintainer has convinced me that the performance hit is unacceptable. I assume by this he means more than 5%, though that is a complete guess on my part.

Perhaps I should elaborate a bit. The 12 copies do a very similar task, but have minute differences. The differences are in various places throughout the function, so unfortunately there are many, many, conditional statements. There are effectively 6 "modes" of operation, and 2 "paradigms" of operation (words made up by myself). To use the function, one specifies the "mode" and "paradigm" of operation. This is never dynamic; each piece of code uses exactly one mode and paradigm. All 12 mode-paradigm pairs are used somewhere in the application. The functions are aptly named func1 to func12, with even numbers representing the second paradigm and odd numbers representing the first paradigm.

I'm aware that this is just about the worst design ever if maintainability is the goal. But it seems to be "fast enough", and this code hasn't needed any changes for a while... It's also worth noting that the original function has not been deleted (although it is dead code as far as I can tell), so refactoring would be simple.

答: You might want to check whether you can still use some sensible abstractions and use e.g. the template method pattern to write understandable code for the common functionality and move the differences between the methods into the "primitive operations" (according to the pattern descrition) in 12 subclasses. This will mightily improve maintainability and testability, and it might actually have the same performance as the current code, since the JVM can inline the method calls for the primitive operations after a while. Of course, you have to check this in a performance test.</p>


问:
Controlling version numbers in sprints
Traditionally software build numbers fit into the format

Major

Minor

Release

Build

Where a Major version is implemented whenever there are breaking changes, Minor when new mini features are added, Release when something is published and Build each time it's built.

I find the last two digits work really well in a CI environment, each CI build increases the Build number and each release to live increments the Release number.

However major waterfall work and breaking change developments are discouraged in more modern methodologies. We prefer to release little and often and so don't tend to make great breaking changes. Given this it's very difficult to determine when to create a new Major version.

We try to avoid making major breaking changes and so are getting very high minor/release numbers but haven't had the excuse to move up a major version yet. What criteria should be used to determine when Major and Minor builds should be made (particularly web based applications)?

I'm aware there are date style versioning but I'm interested in the Major.Minor.Build.Release only.

答: If the user of your software is not able to update to the new version and use it without further manual migration steps (data, configuration, interfaces and the like), that is a strong indicator to increase the major version.</p>

Title: Controlling version numbers in sprints


问:
using this in a constructor, why does it work?
I got two classes.

busiObj(created from ancient flat files)

key(provides means to tell busiObjs apart)

key has a c'tor that takes a busiObj

```<code>public key(busiObj foo)
{
    bar = foo.someThing;
    [...]
}
</code>```

I want to create the key in the c'tor of the busiObj

```<code>public busiObj(...)
{
    myKey = new key(this);
    [...]
}
</code>```

However: Why on earth does it work?

I firmly expected the compiler to throw something like:

"You cannot reference an object that is currently being made, duh".

So: How is it that I can reference an object while it is still being constructed?

答:
So: How is it that I can reference an object while it is still being constructed?

Presumably Because it's useful (though in a dangerous way) for exactly the scenario you describe, so the language designers decided to allow it.

Technically, it's not a problem at all: while the object is being constructed, its memory must already be allocated, so you can certainly have a reference to it.


答:
Whatever you're using internally, externally you're still going to have a waterfall like release cycle.

Whether it's now called a "feature release", "release to production", "customer delivery version", or whatever, it's going to be the combined product of a number of sprints and shorter internal cycles.

That's where the high level numbers come in.

So you'd have a number per nightly build (which might be overkill), a number per sprint, a number per delivery to sales/delivery, and then they put some nice marketing approved label/number on it that's often completely unrelated to anything used internally.

Title: Controlling version numbers in sprints

Body:
Traditionally software build numbers fit into the format

Major

Minor

Release

Build

Where a Major version is implemented whenever there are breaking changes, Minor when new mini features are added, Release when something is published and Build each time it's built.

I find the last two digits work really well in a CI environment, each CI build increases the Build number and each release to live increments the Release number.

However major waterfall work and breaking change developments are discouraged in more modern methodologies. We prefer to release little and often and so don't tend to make great breaking changes. Given this it's very difficult to determine when to create a new Major version.

We try to avoid making major breaking changes and so are getting very high minor/release numbers but haven't had the excuse to move up a major version yet. What criteria should be used to determine when Major and Minor builds should be made (particularly web based applications)?

I'm aware there are date style versioning but I'm interested in the Major.Minor.Build.Release only.


答:
In the example you give in your question, you can't unit test/TDD your way into the situation of clicking the button twice to cause an error very easily. But what you can unit test is that in the code which is called when you click the button, if you get an exception from the persistence layer you handle it appropriately (by either mocking out the persistence layer or by using an in-memory database as has been suggested in other answers) - either by rethrowing or displaying an error or whatever.

You're right that TDD can start to break down when you need to perform tests that aren't a good fit for a unit test (i.e. integration/system tests) - this formed quite a lot of the discussion in the recent "Is TDD Dead?" debates between Kent Beck, Martin Fowler and David Heinemeier Hansson: http://martinfowler.com/articles/is-tdd-dead/


答:
Another answer gave an excellent overview over how you'd nicely encapsulate the row-oriented storage and give a better view. But since you also ask about performance, let me address that: SoA layout is not a silver bullet. It's a pretty good default (for cache usage; not so much for ease of implementation in most languages), but it's not all there is, not even in data oriented design (whatever that exactly means). It's possible that the authors of some introductions you've read missed that point and present only SoA layout because they think that's the entire point of DOD. They'd be wrong, and thankfully not everyone falls into that trap.

As you've probably already realized, not every piece of primitive data benefits from being pulled out into its own array. SoA layout is of advantage when the components that you split into separate arrays are usually accessed separately. But not every tiny piece is accessed in isolation, for example a position vector is almost always read and updated wholesale, so naturally you don't split that one. In fact, your example didn't do that either! Likewise, if you usually access all the properties of a Ball together, because you spend most of your time swapping balls around in your collection of balls, there is no point in separating them.

However, there's a second side to DOD. You don't get all the cache and organization advantages just by turning your memory layout 90° and doing the least to fix the resulting compile errors. There are other common tricks taught under this banner. For example "existence-based processing": If you frequently deactivate balls and re-reactivate them, don't add a flag to the ball object and make the update loop ignore balls with the flag set to false. Move the ball from a "active" collection to a "inactive" collection, and make the update loop only inspect the "active" collection.

More importantly and relevantly to your example: If you spend so much time shuffling the balls array, maybe you are doing something wrong. Why does the order matter? Can you make it not matter? If so, you'd gain several benefits:

You don't need to shuffle the collection (the fastest code is no code at all).

You can add and delete more easily and efficiently (swap to end, drop last).

The remaining code may become eligible for further optimizations (such as the layout change you focus on).

So instead of blindly throwing SoA at everything, think about your data and how you process it. If you find that you process the positions and velocities in one loop, then go through the meshes, and then update the hitpoints, try splitting your memory layout into these three parts. If you find that you access the x, y, z components of the position in isolation, maybe turn your position vectors into a SoA. If you find yourself shuffling data more than actually doing something useful, maybe stop shuffling it.


答: I use what the people who have gone before me in the code base use.  Else I'm with Michael</p>

Title: Coding style - define fields separate or not

Body:
Some use this convention:

```<code>private String event;
public getEvent() { return this.event; }
public setEvent(String value) { this.event = value; }

private int id;
... same as above ...
</code>```

In some projects they define all the fields first, all static fields after (or before), all public/private variables, then the setters, getters, other methods.

Which do you use and why?


答:
Why this happens is because this is a reference to the memory area that your object is currently occupying. Since instructions in the code are executed sequentially, you already have your object in some state that's being referenced when you get to the point of calling the key() method.

Although this is possible, the entire approach is wrong. The methods inside an object should only change the state of the instance, not the state of other instances of that object. Your example works because it roughly translates into the code below at runtime:

```<code>public busiObj() {
    myKey = new key();
    [...]
}

public key()
{
    bar = foo.someThing;
    [... use "this" here to reference your object internally ...]
    [... other stuff here ...]
}
</code>```

Title: using this in a constructor, why does it work?

Body:
I got two classes.

busiObj(created from ancient flat files)

key(provides means to tell busiObjs apart)

key has a c'tor that takes a busiObj

```<code>public key(busiObj foo)
{
    bar = foo.someThing;
    [...]
}
</code>```

I want to create the key in the c'tor of the busiObj

```<code>public busiObj(...)
{
    myKey = new key(this);
    [...]
}
</code>```

However: Why on earth does it work?

I firmly expected the compiler to throw something like:

"You cannot reference an object that is currently being made, duh".

So: How is it that I can reference an object while it is still being constructed?

Body:
Why this happens is because this is a reference to the memory area that your object is currently occupying. Since instructions in the code are executed sequentially, you already have your object in some state that's being referenced when you get to the point of calling the key() method.

Although this is possible, the entire approach is wrong. The methods inside an object should only change the state of the instance, not the state of other instances of that object. Your example works because it roughly translates into the code below at runtime:

public busiObj() {
    myKey = new key();
    [...]
}

public key()
{
    bar = foo.someThing;
    [... use "this" here to reference your object internally ...]
    [... other stuff here ...]
}


答:
From a c# perspective K&R might be a great book on c ... i recently read it in an attempt to brush up on c. I have to say the code in it makes my eyes bleed (when having a c# hat on).

In c# the current style is to make everything clear and not ram 4+ statements into one.

If you want to learn c# this is not the place to start. I cannot recommend a book however. But c, c++ and c# are completely separate languages.

c is amazing in its simplicity and elegance. Its a bit stone age in its processes but going back to its simplicity was something i found rewarding. And highly inspiring because entire operating systems are built on this without all the stuff that is essential to c#

c# is enterprisey and is very good at it.

c++ has evolved so much over the years, learning it from a c perspective is a bad idea as so much has been learnt and rebuilt over time. you want to start from a very modern approach to avoid the horrible bits imo.


问:
How bad is it calling println() often than concatenating strings together and calling it once?
I know output to the console is a costly operation. In the interest of code readability sometimes it is nice to call a function to output text twice, rather than having a long string of text as an argument.

For example how much less efficient is it to have

```<code>System.out.println("Good morning.");
System.out.println("Please enter your name");
</code>```

vs.

```<code>System.out.println("Good morning.\nPlease enter your name");
</code>```

In the example the difference is only one call to println() but what if it's more?

On a related note, statements involving printing text can look strange while viewing the source code if the text to print is long. Assuming the text itself can't be made shorter, what can be done? Should this be a case where multiple println()calls be made? Someone once told me a line of code shouldn't be more than 80 characters (IIRC) so what would you do with

```<code>System.out.println("Good morning everyone. I am here today to present you with a very, very lengthy sentence in order to prove a point about how it looks strange amongst other code.");
</code>```

Is the same true for languages such as C/C++ since each time data is written to an output stream a system call must be made and the process must go to kernel mode (which is very costly)?

答:
There are two 'forces' here, in tension: Performance vs. Readability.

Let's tackle the third problem first though, long lines:

```<code>System.out.println("Good morning everyone. I am here today to present you with a very, very lengthy sentence in order to prove a point about how it looks strange amongst other code.");
</code>```

The best way to implement this and keep readibility, is to use string concatenation:

```<code>System.out.println("Good morning everyone. I am here today to present you "
                 + "with a very, very lengthy sentence in order to prove a "
                 + "point about how it looks strange amongst other code.");
</code>```

The String-constant concatenation will happen at compile time, and will have no effect on performance at all. The lines are readable, and you can just move on.

Now, about the:

```<code>System.out.println("Good morning.");
System.out.println("Please enter your name");
</code>```

vs.

```<code>System.out.println("Good morning.\nPlease enter your name");
</code>```

The second option is significantly faster. I will suggest about 2X as fast.... why?

Because 90% (with a wide margin of error) of the work is not related to dumping the characters to the output, but is overhead needed to secure the output to write to it.

Synchronization

System.out is a PrintStream. All Java implementations that I know of, internally synchronize the PrintStream: See the code on GrepCode!.

What does this mean for your code?

It means that each time you call System.out.println(...) you are synchronizing your memory model, you are checking and waiting for a lock. Any other threads calling System.out will also be locked.

In single-threaded applications the impact of System.out.println() is often limited by the IO performance of your system, how fast can you write out to file. In multithreaded applications, the locking can be more of an issue than the IO.

Flushing

Each println is flushed. This causes the buffers to be cleared and triggers a Console-level write to the buffers. The amount of effort done here is implementation dependant, but, it is generally understood that the performance of the flush is only in small part related to the size of the buffer being flushed. There is a significant overhead related to the flush, where memory buffers are marked as dirty, the Virtual machine is performing IO, and so on. Incurring that overhead once, instead of twice, is an obvious optimization.

Some numbers

I put together the following little test:

```<code>public class ConsolePerf {

    public static void main(String[] args) {
        for (int i = 0; i < 100; i++) {
            benchmark("Warm " + i);
        }
        benchmark("real");
    }

    private static void benchmark(String string) {
        benchString(string + "short", "This is a short String");
        benchString(string + "long", "This is a long String with a number of newlines\n"
                  + "in it, that should simulate\n"
                  + "printing some long sentences and log\n"
                  + "messages.");

    }

    private static final int REPS = 1000;

    private static void benchString(String name, String value) {
        long time = System.nanoTime();
        for (int i = 0; i < REPS; i++) {
            System.out.println(value);
        }
        double ms = (System.nanoTime() - time) / 1000000.0;
        System.err.printf("%s run in%n    %12.3fms%n    %12.3f lines per ms%n    %12.3f chars per ms%n",
                name, ms, REPS/ms, REPS * (value.length() + 1) / ms);

    }

}
</code>```

The code is relatively simple, it repeatedly prints either a short, or a long string to output. The long String has multiple newlines in it. It measures how long it takes to print 1000 iterations of each.

If I run it at the unix (Linux) command-prompt, and redirect the STDOUT to /dev/null, and print the actual results to STDERR, I can do the following:

```<code>java -cp . ConsolePerf > /dev/null 2> ../errlog
</code>```

The output (in errlog) looks like:

```<code>Warm 0short run in
           7.264ms
         137.667 lines per ms
        3166.345 chars per ms
Warm 0long run in
           1.661ms
         602.051 lines per ms
       74654.317 chars per ms
Warm 1short run in
           1.615ms
         619.327 lines per ms
       14244.511 chars per ms
Warm 1long run in
           2.524ms
         396.238 lines per ms
       49133.487 chars per ms
.......
Warm 99short run in
           1.159ms
         862.569 lines per ms
       19839.079 chars per ms
Warm 99long run in
           1.213ms
         824.393 lines per ms
      102224.706 chars per ms
realshort run in
           1.204ms
         830.520 lines per ms
       19101.959 chars per ms
reallong run in
           1.215ms
         823.160 lines per ms
      102071.811 chars per ms
</code>```

What does this mean? Let me repeat the last 'stanza':

```<code>realshort run in
           1.204ms
         830.520 lines per ms
       19101.959 chars per ms
reallong run in
           1.215ms
         823.160 lines per ms
      102071.811 chars per ms
</code>```

It means that, for all intents and purposes, even though the 'long' line is about 5-times longer, and contains multiple newlines, it takes just about as long to output as the short line.

The number of characters-per-second for the long run is 5 times as much, and the elapsed time is about the same.....

In other words, your performance scales relative to the number of printlns you have, not what they print.

Update: What happens if you redirect to a file, instead of to /dev/null?

```<code>realshort run in
           2.592ms
         385.815 lines per ms
        8873.755 chars per ms
reallong run in
           2.686ms
         372.306 lines per ms
       46165.955 chars per ms
</code>```

It is a whole lot slower, but the proportions are about the same....

OwnerDisplayName: Celeritas

Body:
There are two 'forces' here, in tension: Performance vs. Readability.

Let's tackle the third problem first though, long lines:

System.out.println("Good morning everyone. I am here today to present you with a very, very lengthy sentence in order to prove a point about how it looks strange amongst other code.");

The best way to implement this and keep readibility, is to use string concatenation:

System.out.println("Good morning everyone. I am here today to present you "
                 + "with a very, very lengthy sentence in order to prove a "
                 + "point about how it looks strange amongst other code.");

The String-constant concatenation will happen at compile time, and will have no effect on performance at all. The lines are readable, and you can just move on.

Now, about the:

System.out.println("Good morning.");
System.out.println("Please enter your name");

vs.

System.out.println("Good morning.\nPlease enter your name");

The second option is significantly faster. I will suggest about 2X as fast.... why?

Because 90% (with a wide margin of error) of the work is not related to dumping the characters to the output, but is overhead needed to secure the output to write to it.

Synchronization

System.out is a PrintStream. All Java implementations that I know of, internally synchronize the PrintStream: See the code on GrepCode!.

What does this mean for your code?

It means that each time you call System.out.println(...) you are synchronizing your memory model, you are checking and waiting for a lock. Any other threads calling System.out will also be locked.

In single-threaded applications the impact of System.out.println() is often limited by the IO performance of your system, how fast can you write out to file. In multithreaded applications, the locking can be more of an issue than the IO.

Flushing

Each println is flushed. This causes the buffers to be cleared and triggers a Console-level write to the buffers. The amount of effort done here is implementation dependant, but, it is generally understood that the performance of the flush is only in small part related to the size of the buffer being flushed. There is a significant overhead related to the flush, where memory buffers are marked as dirty, the Virtual machine is performing IO, and so on. Incurring that overhead once, instead of twice, is an obvious optimization.

Some numbers

I put together the following little test:

public class ConsolePerf {

    public static void main(String[] args) {
        for (int i = 0; i < 100; i++) {
            benchmark("Warm " + i);
        }
        benchmark("real");
    }

    private static void benchmark(String string) {
        benchString(string + "short", "This is a short String");
        benchString(string + "long", "This is a long String with a number of newlines\n"
                  + "in it, that should simulate\n"
                  + "printing some long sentences and log\n"
                  + "messages.");

    }

    private static final int REPS = 1000;

    private static void benchString(String name, String value) {
        long time = System.nanoTime();
        for (int i = 0; i < REPS; i++) {
            System.out.println(value);
        }
        double ms = (System.nanoTime() - time) / 1000000.0;
        System.err.printf("%s run in%n    %12.3fms%n    %12.3f lines per ms%n    %12.3f chars per ms%n",
                name, ms, REPS/ms, REPS * (value.length() + 1) / ms);

    }

}

The code is relatively simple, it repeatedly prints either a short, or a long string to output. The long String has multiple newlines in it. It measures how long it takes to print 1000 iterations of each.

If I run it at the unix (Linux) command-prompt, and redirect the STDOUT to /dev/null, and print the actual results to STDERR, I can do the following:

java -cp . ConsolePerf > /dev/null 2> ../errlog

The output (in errlog) looks like:

Warm 0short run in
           7.264ms
         137.667 lines per ms
        3166.345 chars per ms
Warm 0long run in
           1.661ms
         602.051 lines per ms
       74654.317 chars per ms
Warm 1short run in
           1.615ms
         619.327 lines per ms
       14244.511 chars per ms
Warm 1long run in
           2.524ms
         396.238 lines per ms
       49133.487 chars per ms
.......
Warm 99short run in
           1.159ms
         862.569 lines per ms
       19839.079 chars per ms
Warm 99long run in
           1.213ms
         824.393 lines per ms
      102224.706 chars per ms
realshort run in
           1.204ms
         830.520 lines per ms
       19101.959 chars per ms
reallong run in
           1.215ms
         823.160 lines per ms
      102071.811 chars per ms

What does this mean? Let me repeat the last 'stanza':

realshort run in
           1.204ms
         830.520 lines per ms
       19101.959 chars per ms
reallong run in
           1.215ms
         823.160 lines per ms
      102071.811 chars per ms

It means that, for all intents and purposes, even though the 'long' line is about 5-times longer, and contains multiple newlines, it takes just about as long to output as the short line.

The number of characters-per-second for the long run is 5 times as much, and the elapsed time is about the same.....

In other words, your performance scales relative to the number of printlns you have, not what they print.

Update: What happens if you redirect to a file, instead of to /dev/null?

realshort run in
           2.592ms
         385.815 lines per ms
        8873.755 chars per ms
reallong run in
           2.686ms
         372.306 lines per ms
       46165.955 chars per ms

It is a whole lot slower, but the proportions are about the same....

OwnerDisplayName: rolfl


答:
In higher level languages like C and C++, this is less of a problem than in Java.

First of all, C and C++ define compile-time string concatenation, so you can so something like:

```<code>std::cout << "Good morning everyone. I am here today to present you with a very, "
    "very lengthy sentence in order to prove a point about how it looks strange "
    "amongst other code.";
</code>```

In such a case, concatenating the string isn't just an optimization you can pretty much, usually (etc.) depend on the compiler to make. Rather, it's directly required by the C and C++ standards (phase 6 of translation: "Adjacent string literal tokens are concatenated.").

Though it's at the expense of a little extra complexity in the compiler and implementation, C and C++ do a bit more to hide the complexity of producing output efficiently from the programmer. Java is much more like assembly language--each call to System.out.println translates much more directly to a call to the underlying operating to write the data to the console. If you want buffering to improve efficiency, that has to be provided separately.

This means, for example, that in C++, rewriting the previous example, to something like this:

```<code>std::cout << "Good morning everyone. I am here today to present you with a very, ";
std::cout << "very lengthy sentence in order to prove a point about how it looks ";
std::cout << "strange amongst other code.";
</code>```

...would normally1 have almost no effect on efficiency. Each use of cout would simply deposit data into a buffer. That buffer would be flushed to the underlying stream when the buffer filled up, or the code tried to read input from the use (such as with std::cin).

iostreams also have a sync_with_stdio property that determines whether output from iostreams is synchronized with C-style input (e.g., getchar). By default sync_with_stdio is set to true, so if, for example, you write to std::cout, then read via getchar, the data you wrote to cout will be flushed when getchar is called. You can set sync_with_stdio to false to disable that (usually done to improve performance).

sync_with_stdio also controls a degree of synchronization between threads. If synchronization is turned on (the default) writing to an iostream from multiple threads can result in the data from the threads being interleaved, but prevents any race conditions. IOW, your program will run and produce output, but if more than one thread writes to a stream at a time, the arbitrary intermixing of the data from the different threads will typically render the output pretty useless.

If you turn off the synchronization, then synchronizing access from multiple threads becomes entirely your responsibility as well. Concurrent writes from multiple threads can/will lead to a data race, which means the code has undefined behavior.

Summary

C++ defaults to an attempt at balancing speed with safety. The result is fairly successful for single-threaded code, but less so for multi-threaded code. Multithreaded code typically needs to ensure that only one thread writes to a stream at a time to produce useful output.

1. It's possible to turn off buffering for a stream, but actually doing so is pretty unusual, and when/if somebody does it, it's probably for a fairly specific reason, such as ensuring that all output is captured immediately despite the effect on performance. In any case, this only happens if the code does it explicitly.

Title: How bad is it calling println() often than concatenating strings together and calling it once?

Body:
I know output to the console is a costly operation. In the interest of code readability sometimes it is nice to call a function to output text twice, rather than having a long string of text as an argument.

For example how much less efficient is it to have

```<code>System.out.println("Good morning.");
System.out.println("Please enter your name");
</code>```

vs.

```<code>System.out.println("Good morning.\nPlease enter your name");
</code>```

In the example the difference is only one call to println() but what if it's more?

On a related note, statements involving printing text can look strange while viewing the source code if the text to print is long. Assuming the text itself can't be made shorter, what can be done? Should this be a case where multiple println()calls be made? Someone once told me a line of code shouldn't be more than 80 characters (IIRC) so what would you do with

```<code>System.out.println("Good morning everyone. I am here today to present you with a very, very lengthy sentence in order to prove a point about how it looks strange amongst other code.");
</code>```

Is the same true for languages such as C/C++ since each time data is written to an output stream a system call must be made and the process must go to kernel mode (which is very costly)?

Body:
In higher level languages like C and C++, this is less of a problem than in Java.

First of all, C and C++ define compile-time string concatenation, so you can so something like:

std::cout << "Good morning everyone. I am here today to present you with a very, "
    "very lengthy sentence in order to prove a point about how it looks strange "
    "amongst other code.";

In such a case, concatenating the string isn't just an optimization you can pretty much, usually (etc.) depend on the compiler to make. Rather, it's directly required by the C and C++ standards (phase 6 of translation: "Adjacent string literal tokens are concatenated.").

Though it's at the expense of a little extra complexity in the compiler and implementation, C and C++ do a bit more to hide the complexity of producing output efficiently from the programmer. Java is much more like assembly language--each call to System.out.println translates much more directly to a call to the underlying operating to write the data to the console. If you want buffering to improve efficiency, that has to be provided separately.

This means, for example, that in C++, rewriting the previous example, to something like this:

std::cout << "Good morning everyone. I am here today to present you with a very, ";
std::cout << "very lengthy sentence in order to prove a point about how it looks ";
std::cout << "strange amongst other code.";

...would normally1 have almost no effect on efficiency. Each use of cout would simply deposit data into a buffer. That buffer would be flushed to the underlying stream when the buffer filled up, or the code tried to read input from the use (such as with std::cin).

iostreams also have a sync_with_stdio property that determines whether output from iostreams is synchronized with C-style input (e.g., getchar). By default sync_with_stdio is set to true, so if, for example, you write to std::cout, then read via getchar, the data you wrote to cout will be flushed when getchar is called. You can set sync_with_stdio to false to disable that (usually done to improve performance).

sync_with_stdio also controls a degree of synchronization between threads. If synchronization is turned on (the default) writing to an iostream from multiple threads can result in the data from the threads being interleaved, but prevents any race conditions. IOW, your program will run and produce output, but if more than one thread writes to a stream at a time, the arbitrary intermixing of the data from the different threads will typically render the output pretty useless.

If you turn off the synchronization, then synchronizing access from multiple threads becomes entirely your responsibility as well. Concurrent writes from multiple threads can/will lead to a data race, which means the code has undefined behavior.

Summary

C++ defaults to an attempt at balancing speed with safety. The result is fairly successful for single-threaded code, but less so for multi-threaded code. Multithreaded code typically needs to ensure that only one thread writes to a stream at a time to produce useful output.

1. It's possible to turn off buffering for a stream, but actually doing so is pretty unusual, and when/if somebody does it, it's probably for a fairly specific reason, such as ensuring that all output is captured immediately despite the effect on performance. In any case, this only happens if the code does it explicitly.

OwnerDisplayName: Jerry Coffin


问:
Android Design - Service vs Thread for Networking
I am writing an Android app, finally (yay me) and for this app I need persistant, but user closeable, network sockets (yes, more than one).  I decided to try my hand at writing my own version of an IRC Client.

My design issue however, is I'm not sure how to run the Socket connectivity itself.  If I put the sockets at the Activity level, they keeps getting closed shortly after the Activity becomes non-visible (also a problem that needs solving...but I think i figured that one out)...but if I run a "connectivity service", I need to find out if I can have multiple instances of it running (the service, that is...one per server/socket).  Either that or a I need a way to Thread the sockets themselves and have multiple threads running that I can still communicate with directly (ID system of some sort).

Thus the question: Is it a 'better', or at least more "proper" design pattern, to put the Socket and networking in a service, and have the Activities consume said service...or should I tie the sockets directly to some Threaded Process owned by the UI Activity and not bother with the service implementation at all?  I do know better than to put the networking directly on the UI thread, but that's as far as I've managed to get.

答:
The answer is Service + Thread.

Don't put the socket in an Activity. The activity is for UI. It gets recreated when the user rotates the phone. You can run into problems when the user receives a phone call.

Put your sockets in a Service. Your activity/activities can bind to the Service and use it's methods. Or you can use a Messenger to communicate between an Activity and the Service.

Services on Android run in the UI thread so in the Service you'll need your own thread for the socket communication.


问:
Is it feasible and useful to auto-generate some code of unit tests?
Earlier today I have come up with an idea, based upon a particular real use case, which I would want to have checked for feasability and usefulness. This question will feature a fair chunk of Java code, but can be applied to all languages running inside a VM, and maybe even outside. While there is real code, it uses nothing language-specific, so please read it mostly as pseudo code.

The idea

Make unit testing less cumbersome by adding in some ways to autogenerate code based on human interaction with the codebase. I understand this goes against the principle of TDD, but I don't think anyone ever proved that doing TDD is better over first creating code and then immediatly therafter the tests. This may even be adapted to be fit into TDD, but that is not my current goal.

To show how it is intended to be used, I'll copy one of my classes here, for which I need to make unit tests.

```<code>public class PutMonsterOnFieldAction implements PlayerAction {
    private final int handCardIndex;
    private final int fieldMonsterIndex;

    public PutMonsterOnFieldAction(final int handCardIndex, final int fieldMonsterIndex) {
        this.handCardIndex = Arguments.requirePositiveOrZero(handCardIndex, "handCardIndex");
        this.fieldMonsterIndex = Arguments.requirePositiveOrZero(fieldMonsterIndex, "fieldCardIndex");
    }

    @Override
    public boolean isActionAllowed(final Player player) {
        Objects.requireNonNull(player, "player");
        Hand hand = player.getHand();
        Field field = player.getField();
        if (handCardIndex >= hand.getCapacity()) {
            return false;
        }
        if (fieldMonsterIndex >= field.getMonsterCapacity()) {
            return false;
        }
        if (field.hasMonster(fieldMonsterIndex)) {
            return false;
        }
        if (!(hand.get(handCardIndex) instanceof MonsterCard)) {
            return false;
        }
        return true;
    }

    @Override
    public void performAction(final Player player) {
        Objects.requireNonNull(player);
        if (!isActionAllowed(player)) {
            throw new PlayerActionNotAllowedException();
        }
        Hand hand = player.getHand();
        Field field = player.getField();
        field.setMonster(fieldMonsterIndex, (MonsterCard)hand.play(handCardIndex));
    }
}
</code>```

We can observe the need for the following tests:

Constructor test with valid input

Constructor test with invalid inputs

isActionAllowed test with valid input

isActionAllowed test with invalid inputs

performAction test with valid input

performAction test with invalid inputs

My idea mainly focuses on the isActionAllowed test with invalid inputs. Writing these tests is not fun, you need to ensure a number of conditions and you check whether it really returns false, this can be extended to performAction, where an exception needs to be thrown in that case.

The goal of my idea is to generate those tests, by indicating (through GUI of IDE hopefully) that you want to generate tests based on a specific branch.

The implementation by example

User clicks on "Generate code for branch if (handCardIndex >= hand.getCapacity())".

Now the tool needs to find a case where that holds.

(I haven't added the relevant code as that may clutter the post ultimately)

To invalidate the branch, the tool needs to find a handCardIndex and hand.getCapacity() such that the condition >= holds.

It needs to construct a Player with a Hand that has a capacity of at least 1.

It notices that the capacity private int of Hand needs to be at least 1.

It searches for ways to set it to 1. Fortunately it finds a constructor that takes the capacity as an argument. It uses 1 for this.

Some more work needs to be done to succesfully construct a Player instance, involving the creation of objects that have constraints that can be seen by inspecting the source code.

It has found the hand with the least capacity possible and is able to construct it.

Now to invalidate the test it will need to set handCardIndex = 1.

It constructs the test and asserts it to be false (the returned value of the branch)

What does the tool need to work?

In order to function properly, it will need the ability to scan through all source code (including JDK code) to figure out all constraints. Optionally this could be done through the javadoc, but that is not always used to indicate all constraints. It could also do some trial and error, but it pretty much stops if you cannot attach source code to compiled classes.

Then it needs some basic knowledge of what the primitive types are, including arrays. And it needs to be able to construct some form of "modification trees". The tool knows that it needs to change a certain variable to a different value in order to get the correct testcase. Hence it will need to list all possible ways to change it, without using reflection obviously.

What this tool will not replace is the need to create tailored unit tests that tests all kinds of conditions when a certain method actually works. It is purely to be used to test methods when they invalidate constraints.

My questions:

Is creating such a tool feasible? Would it ever work, or are there some obvious problems?

Would such a tool be useful? Is it even useful to automatically generate these testcases at all? Could it be extended to do even more useful things?

Does, by chance, such a project already exist and would I be reinventing the wheel?

If not proven useful, but still possible to make such thing, I will still consider it for fun. If it's considered useful, then I might make an open source project for it depending on the time.

For people searching more background information about the used Player and Hand classes in my example, please refer to this repository. At the time of writing the PutMonsterOnFieldAction has not been uploaded to the repo yet, but this will be done once I'm done with the unit tests.

答:
It's all just software, so given enough effort it's possible ;-). In a language which supports a decent way of doing code analysis it should be feasible too.

As for the usefulness, I think it some automation around unit testing is useful to a certain level, depending on how it's implemented. You need to be clear about where you want to go in advance, and be very aware of the limitations of this type of tooling.

However, the flow you describe has a huge limitation, because the code being tested leads the test. This means an error in reasoning when developing the code will likely end up in the test as well. The end result will probably be a test which basically confirms the code 'does what it does' instead of confirming it does what it should do. This actually isn't completely useless because it can be used later to verify the code still does what it did earlier, but it isn't a functional test and you shouldn't consider your code tested based on such a test. (You might still catch some shallow bugs, like null handling etc.) Not useless, but it can't replace a 'real' test. If that means you'll still have to create the 'real' test it might not be worth the effort.

You could go slightly different routes with this though. The first one is just throwing data at the API to see where it breaks, perhaps after defining some generic assertions about the code. That's basically Fuzz testing

The other one would be to generate the tests but without the assertions, basically skipping step 10. So end up with an 'API quiz' where your tools determines useful test cases and asks the tester for the expected answer given a specific call. That way you're actually testing again. It's still not complete though, if you forget a functional scenario in your code the tool won't magically find it and it doesn't remove all assumptions. Suppose the code should have been if (handCardIndex > hand.getCapacity()) instead if >=, a tool like this will never figure that out by itself.
If you want to go back to TDD you could suggest test cases based on just the interface, but that would be even more functionally incomplete, because there isn't even code from which you can infer some functionality.

Your main issues are always going to be 1. Carrying errors in code over to the test and 2. functional completeness. Both issues can be suppressed somewhat, be never be eliminated. You'll always have to go back to the requirements and sit down to verify they are all actually tested correctly. The clear danger here is a false sense of security because your code coverage shows 100% coverage. Sooner or later someone will make that mistake, it's up to you to decide if the benefits outweigh that risk. IMHO it boils down to a classic trade-off between quality and development speed.


问:
Any real use of pointers in C#?
What is a situation while coding in C# where using pointers is a good or necessary option? I'm talking about unsafe pointers.

答: I once had to use pointers (in the unsafe context) in a C# based windows application that would act as an interface to a headset. This application is a user interface that would allow agents (at a call center) to control their headphone settings. This application acts as an alternate to the control panel given by the headset manufacturer. Thus, their ability to control the headsets were limited when compared to the options available. I had to use pointers because I had to use the API (a Visual C++ dll) provided by the headset manufacturer using P/Invoke.</p>

Title: Any real use of pointers in C#?


问:
What is a good C++ API Design for HW registers?
I am designing an API for a driver that manipulates HW. I have done the following:

```<code>namespace HWRegister
{
    //private:
    namespace Data
    {
        //accessible only within this namespace

        //Represents one of the four HW blocks.
        enum EHWUnit
        {
            Block0,
            Block1,
            Block2,
            Block3
        };

        enum EHWSet
        {
            Rx0,
            Rx1,
            Rx2,
            Rx3,
            Tx0,
            Tx1,
            Tx2,
            Tx3
        };
    }

    using namespace Data;

    //Returns Error Code
    int32_t enableHWUnit( const EHWUnit aHWNumber, const EHWSet aHWSet );
    //Returns Error Code.
    int32_t disableHWUnit( const EHWUnit aHWNumber );
</code>```

I would like to get peoples opinion on this. I am wondering if those enums are a good idea, I declare them here and therefore people who use my API are forced to use them, will this cause more problems for the callers of my API? Can I improve this API somehow? I will add more documentation into the API when I am sure it is looking good.

答:
Enums are a good idea

If you'll provide this code via dll (good idea for me), I'd recommend not to change them into enum class and not no throw the exception, because these are C++ only features, and users will be not able to use your dll in C# for example

If these enums will be used as a real register address (or offset) it would be better to initialize each member of enum with a particular value (even if they are 0, 1, 2, etc.) in hex fromat (it's agreed notation in HW - driver - user program bundle)

```<code>enum EHWSet
{
    Rx0 = 0x00,
    Rx1 = 0x01,
    Rx2 = 0x02,
    Rx3 = 0x03,
    Tx0 = 0x04,
    Tx1 = 0x05,
    Tx2 = 0x06,
    Tx3 = 0x07
};
</code>```

Exclude using namespace from header. It's a very very very bad idea

Body:
Enums are a good idea

If you'll provide this code via dll (good idea for me), I'd recommend not to change them into enum class and not no throw the exception, because these are C++ only features, and users will be not able to use your dll in C# for example

If these enums will be used as a real register address (or offset) it would be better to initialize each member of enum with a particular value (even if they are 0, 1, 2, etc.) in hex fromat (it's agreed notation in HW - driver - user program bundle)

enum EHWSet
{
    Rx0 = 0x00,
    Rx1 = 0x01,
    Rx2 = 0x02,
    Rx3 = 0x03,
    Tx0 = 0x04,
    Tx1 = 0x05,
    Tx2 = 0x06,
    Tx3 = 0x07
};

Exclude using namespace from header. It's a very very very bad idea