The Truth About Structuring an HTML5 Page:
Part polemic, part instruction manual, The Truth About HTML5 has ignited an interesting debate. Here we present an exclusive excerpt on the problems around structuring in HTML5
Monday, October 8, 2012
Saturday, June 2, 2012
Best method of learning Javascript?
Best method of learning Javascript?:
[link] [21 comments]
For some reason Javascript has been an extremely annoying language for me. I learned HTML and CSS without a hitch and I'm fairly decent with PHP. It seems like every book I read on Javascript never really goes in depth with how the syntax works and instead just gives you a bunch of examples. I'll get through a good chunk of a book and attempt to make even a simple script for a site...and have no idea whatsoever how. Anyone have any resources they maybe used to learn? Book Recommendations?
submitted by lordolunch [link] [21 comments]
Friday, June 1, 2012
Enterprise Resource Planning (ERP): What Are The Benefits Of Hosted/Cloud ERP Software
Enterprise Resource Planning (ERP): What Are The Benefits Of Hosted/Cloud ERP Software: Enterprise Resource Planning systems have changed a great deal over the years. Since cloud-based services have gained popularity, enterprise hosted solutions have had a high adoption rate. Although it is still a relatively new technology, cloud solutions are considered to be the way forward for enterprise resource planning (ERP) implementations. There are many advantages to [...]
.NET Performance Profiling: Learn the Skills, Write Better Code
.NET Performance Profiling: Learn the Skills, Write Better Code: In Practical Performance Profiling MVP Jean-Philippe explains how the .NET platform manages memory and uses a sample application based on a real software package to demonstrate a wide range of performance slowdowns
Monday, May 21, 2012
The Rapid Research Method
The Rapid Research Method:
The Rapid Research Method is a way to speed up your product research. It’s also a way to speed up ramp up time when you are leaning a new domain. The Rapid Research Method is also a key for rapid innovation and rapid product design and development. Lastly, the Rapid Research Method is also a great way to map out a space and perform competitive assessments.
One of the challenges with product development is doing effective research for your product design to make sure you have the right map of the pains and needs, the top concerns, and the key desired outcomes. Another challenge is actually making this information actionable and simple to share.
I’ve had the benefit of driving several projects end-to-end, so I’ve been through the research and exploration stage multiple times. I’ve learned a lot of tricks for speeding up research and making it more effective. I’ve had to use these techniques to play catch up in various domains from application architecture to security and performance, to even the cloud. They work.
I’m going to share a few techniques in this post. Collectively, I”ll refer to using them as the Rapid Research Method. It’s the approach I’ve used for many, many projects over many years, and as a way to perform competitive assessments.
What’s important about the techniques is that they make it easy to rapidly organize and share vast amounts of information in an actionable way. Looking back, one of the big surprises for me is how just about any domain can be broken down into questions and tasks. If you know the questions that people ask and the tasks they need to perform, you’ve effectively mapped out the most important information within that domain. This helps you prioritize all the rest of the information, such as concepts, principles, patterns, and practices. Another way to look at it is that all the information is either going to be action or reference. For example, a checklist would be actionable, while a whitepaper on a key topic, tends to be conceptual.
Software, like an information product, tends to suffer from information management problems. It’s tough to share “castles in the mind.” Then there is the people factor. Not everybody can slice and dice information the same way, or with the same skill. The real issue though is sharing “state.” The problem with research is that it’s like climbing a mountain. How quickly can you get others to make it up the mountain, after you? What sort of trail or spikes can you leave along the way? That’s where these research tools that I’m about to share come into play. They help you not only get you and your teammates up the mountain faster, but they leave a trail that others can follow.
Here is the approach in a nutshell:
You can browse the examples below to see what these question lists, task lists, hot spots/frame, and user stories look like.
The Frame is simply a lens for looking at a problem. It’s what’s in the picture and what’s out. How you frame a problem domain can either simplify the problem space, or make it more complex. When you frame the problem space well, it makes it easier to act on it. It makes it easier to identify opportunities for innovation. It makes it easier to research the problem space with better focus. Focus is your friend.
The problem is that you usually don’t know the key areas up front. Framing out the space is part of the challenge and it’s part of the by-product of your research. What I’ve found is that when you start to collect questions and tasks, that “Hot Spots” start to emerge. You will quickly start to see patterns and things will naturally start to cluster. This collection of “Hot Spots” becomes the backbone for your frame. Rather than be complete, it’s about being effective. You can use the 80/20 rule to your advantage here, which is how you both gain speed, but also amplify your impact by focusing on the highest priorities.
Security Frame with Hot Spots
Categories
Architectural Frame Questions List
Contents
Architectural Frame Tasks List
Categories
The beauty is that when you capture the user stories well, it is very easy to deal with both timeless stories and timely ones. In this particular example, even though it’s a few years old, you can see that the top issues that it exposes are alive and well. One additional point on this example is that I used another information pattern. I call it the “View More” pattern. I use it to bubble up the short-list and then push the rest of the list below the “View More …” heading. It’s highly effective for organizing very large information sets, especially if you alphabetize the list.
User Stories for Cloud Enterprise Strategy
Categories

The Rapid Research Method is a way to speed up your product research. It’s also a way to speed up ramp up time when you are leaning a new domain. The Rapid Research Method is also a key for rapid innovation and rapid product design and development. Lastly, the Rapid Research Method is also a great way to map out a space and perform competitive assessments.
One of the challenges with product development is doing effective research for your product design to make sure you have the right map of the pains and needs, the top concerns, and the key desired outcomes. Another challenge is actually making this information actionable and simple to share.
I’ve had the benefit of driving several projects end-to-end, so I’ve been through the research and exploration stage multiple times. I’ve learned a lot of tricks for speeding up research and making it more effective. I’ve had to use these techniques to play catch up in various domains from application architecture to security and performance, to even the cloud. They work.
I’m going to share a few techniques in this post. Collectively, I”ll refer to using them as the Rapid Research Method. It’s the approach I’ve used for many, many projects over many years, and as a way to perform competitive assessments.
What’s important about the techniques is that they make it easy to rapidly organize and share vast amounts of information in an actionable way. Looking back, one of the big surprises for me is how just about any domain can be broken down into questions and tasks. If you know the questions that people ask and the tasks they need to perform, you’ve effectively mapped out the most important information within that domain. This helps you prioritize all the rest of the information, such as concepts, principles, patterns, and practices. Another way to look at it is that all the information is either going to be action or reference. For example, a checklist would be actionable, while a whitepaper on a key topic, tends to be conceptual.
Software, like an information product, tends to suffer from information management problems. It’s tough to share “castles in the mind.” Then there is the people factor. Not everybody can slice and dice information the same way, or with the same skill. The real issue though is sharing “state.” The problem with research is that it’s like climbing a mountain. How quickly can you get others to make it up the mountain, after you? What sort of trail or spikes can you leave along the way? That’s where these research tools that I’m about to share come into play. They help you not only get you and your teammates up the mountain faster, but they leave a trail that others can follow.
About the Approach
The approach is fairly easy. It involves creating simple lists. The power comes from how you create and share these lists. It’s actually the information architecture of the research that unleashes the power of your research. The single best thing you can do with your research is produce output that can easily be used by others, so that you can easily bring in more brains on the problem. When everybody can see the lay of the land, it’s easier for people to find a faster way forward, get resourceful and solve problems.Here is the approach in a nutshell:
- Gather the Questions. In this step, simply start gathering the user questions. Questions are everywhere. The trick is to capture them and put them down on paper. My favorite questions are “Why" and “What is XYZ" and “How does XYZ work” or “When do I use XYZ.”
- Gather the Tasks. In this step, simply gather the user tasks. You can interview users. You can watch them in action. You can survey. You can play and explore the domain yourself. You can analyze search queries. What’s important is that you capture the user actions. This compliments the user questions. Questions are the “conceptual.” Tasks are the “actionable.” When gathering tasks, I find it helpful to write them down individually using the pattern “How To XYZ.”
- Identify the Hot Spots. As you organize your questions list and tasks list into more meaningful buckets, you’ll start to see common categories. Consolidate the categories as best you can. This will help you focus and refine your research and funnel what you learn. These “Hot Spots” will make it a lot easier to slice and dice the domain into actionable nuggets.
- Create a Frame. Use your “Hot Spots” to create a “Frame” for your domain. One way to do this is to create a simple table of your “Hot Spots” and a description of each “Hot Spot.” This creates a quick lens for looking at the domain, and puts a focus on the most important categories of information. When you need to share your research with others, you now have a fast way to show how you broke the information space down and made it more actionable. Experts will quickly validate or correct your frame. That’s the beauty of this approach. You can’t lose. You are always improving it based on what you learn. It’s a true learning system.
- Gather User Stories. User stories are a great way to really take things to the next level. They help to up-level the tasks, build empathy, and really put things in context. I like to manage these as simple lists, and I use the language, “As a user, I want to …”, or “As a user, I need to …”
You can browse the examples below to see what these question lists, task lists, hot spots/frame, and user stories look like.
Key Guidelines to Keep in Mind
- The value of your solution is the value of the problem solved. So the better job you do of capturing the right problems, the better chance you have at a successful product or solution.
- Experience is the short cut. Find the people with experience. You can save yourself exponential time, money, and resources by finding the right people who have the experience who can quickly share the questions, tasks, and scenarios that matter within a given domain.
- Relevancy is king. The best solutions don’t matter if they aren’t relevant. You stay relevant by staying connected. The trick is to stay connected to the key opinion leaders and influencers within a given domain. You want to know the movers and the shakers as well as the folks that play in the domain on a daily basis.
Hot Spots and Frames
“Hot Spots” are simply the key categories or areas of focus. They represent the categories that are key choice points. They are actionable. They are “Hot Spots” because they are 80% of where the action is. They are the 20% of the domain that accounts for 80% of the activity. I use “Hot Spots” as a way to slice a domain down to size and quickly get to what counts. Each “Hot Spots” represents an area that is either a key opportunity or a key pain point. The “Hot Spots” are a great way to organize actionable information such as principles, patterns, and practices.The Frame is simply a lens for looking at a problem. It’s what’s in the picture and what’s out. How you frame a problem domain can either simplify the problem space, or make it more complex. When you frame the problem space well, it makes it easier to act on it. It makes it easier to identify opportunities for innovation. It makes it easier to research the problem space with better focus. Focus is your friend.
The problem is that you usually don’t know the key areas up front. Framing out the space is part of the challenge and it’s part of the by-product of your research. What I’ve found is that when you start to collect questions and tasks, that “Hot Spots” start to emerge. You will quickly start to see patterns and things will naturally start to cluster. This collection of “Hot Spots” becomes the backbone for your frame. Rather than be complete, it’s about being effective. You can use the 80/20 rule to your advantage here, which is how you both gain speed, but also amplify your impact by focusing on the highest priorities.
Frame Example
This is a simple example of a frame using security Hot Spots. By using this collection of Hot Spots, it was very easy to collect questions and tasks within the security domain. It was also easy to walk different technologies and evaluate their security profile. We also used the frame to quickly gather and organize threats, attacks, vulnerabilities, and countermeasures. Organizing the information using this frame made it more actionable, and it made it a lot easier to deal with information overload.Security Frame with Hot Spots
Categories
- Auditing and Logging
- Authentication
- Configuration Management
- Cryptography
- Exception Management
- Input and Data Validation
- Sensitive Data
- Session Management
| Category | Key Considerations |
| Auditing and Logging | Who did what and when? Auditing and logging refer to how your application records security-related events. |
| Authentication | Who are you? Authentication is the process where an entity proves the identity of another entity, typically through credentials, such as a user name and password. |
| Authorization | What can you do? Authorization is how your application provides access controls for resources and operations. |
| Configuration Management | Who does your application run as? Which databases does it connect to? How is your application administered? How are these settings secured? Configuration management refers to how your application handles these operational issues. |
| Cryptography | How are you keeping secrets (confidentiality)? How are you tamper-proofing your data or libraries (integrity)? How are you providing seeds for random values that must be cryptographically strong? Cryptography refers to how your application enforces confidentiality and integrity. |
| Exception Management | When a method call in your application fails, what does your application do? How much do you reveal? Do you return friendly error information to end users? Do you pass valuable exception information back to the caller? Does your application fail gracefully? |
| Input and Data Validation | How do you know that the input your application receives is valid and safe? Input validation refers to how your application filters, scrubs, or rejects input before additional processing. Consider constraining input through entry points and encoding output through exit points. Do you trust data from sources such as databases and file shares? |
| Sensitive Data | How does your application handle sensitive data? Sensitive data refers to how your application handles any data that must be protected either in memory, over the network, or in persistent stores. |
| Session Management | How does your application handle and protect user sessions? A session refers to a series of related interactions between a user and your Web application. |
Question List Example
A “Question List” is simply a list of the key questions that people ask. You can find the key questions through surveys, going through forums, looking through blogs, and through hands on experience. Hands on experience helps you build empathy for what really matters, which will be essential when you are trying to rank, rate, and sort your list. It also helps to organize your questions into “Hot Spot” areas or buckets.Architectural Frame Questions List
Contents
- Authentication and Authorization
- Caching and State
- Communication
- Composition
- Concurrency and Transactions
- Configuration Management
- Coupling and Cohesion
- Data Access
- Exception Management
- Logging and Instrumentation
- User Experience
- Validation
- Workflow
- What are the approaches for identity store?
- What authentication mechanism will be used?
- What are relevant authentication and authorization patterns?
- How do I decide if I need to implement single sign-on?
- How do I design frontend single sign-on?
- How do I design for backend single sign-on?
- How to I flow identity to backend?
- What is impersonation and when should I use it?
- What is delegation and constrained delegation?
- How do you refresh your cache?
- How to design effective caching mechanism?
- What are common architectural pitfalls with caching?
- Which layers should implement caching?
- What data should be cached? (presentation, business and data access layers)
- How to choose a cache be store?
- How to cache data with different logical scopes?
- How to cache data on client side?
- How to cache user specific data?
- How to decide whether caching data will improve performance?
- How to manage expiration policy and scavenging mechanism?
- How do I protect cached data?
- How do I structure my application to optimize communication efficiency?
- How do I protect my communication channels?
- How do I choose appropriate communication protocol?
- How to flow identity across layers (tiers)?
- What are the common architectural pitfalls with communication?
- How do I structure my application to optimize communication efficiency?
- How do I protect my communication channels?
- How do I choose appropriate communication protocol?
- How to flow identity across layers (tiers)?
- What are the common architectural pitfalls with communication?
- What are effective techniques for communication between the tiers?
- What are effective techniques for exchanging data between the tiers?
- How to secure communication between the layers?
- When should I use synchronous communication?
- When should I use asynchronous communication?
- What are effective transaction management strategies?
- How do I determine concurrency requirements?
- What are the common architectural pitfalls with transactions?
- What’s the right level of granularity of transactions?
- How do I manage distributed transactions?
- What are the common architectural pitfalls with concurrency?
- How do I manage per app configuration?
- How do I manage per user configuration?
- How do I synchronize configuration across distributed environment (solutions)
- What are the effective configuration management patterns?
- What are the common architectural pitfalls with configuration?
- How to determine effective layering strategy?
- What are effective design patterns for coupling and cohesion?
- How to determine appropriate coupling between components and between layers?
- What are the common architectural pitfalls with coupling and cohesion?
- How do we pass data through the layers (tiers)?
- How to design effective data abstraction layer?
- How to model the data?
- How to page records?
- How to design for very large databases?
- What are the common architectural pitfalls with data access?
- What is domain-driven architecture?
- What is a database-driven architecture?
- How do I choose between domain-driven and database-driven?
- Which technology should be used to access data store?
- How to manage database connections?
- What logic should be implemented in the data helper components?
- How to handle transactions?
- How to handle concurrency?
- How to design for a multi-tenant database?
- How to choose between in-line SQL and Stored Procedures?
- How to design effective exception management strategy?
- What are effective exception management patterns?
- What are the common architectural pitfalls with exception management?
- How to design effective exception management strategy?
- What are effective exception management patterns?
- What are the common architectural pitfalls with exception management?
- Whether to design custom exception handling logic?
- Which layers should implement exception management?
- How to deal with unhandled exceptions?
- How to display exception info to users?
- How to design logging of exception data?
- How to propagate exceptions through layers?
- How to design effective layering strategy?
- What are effective design patterns for layering?
- How to separate layers into areas of concerns?
- How should layers interact?
- What are the common architectural pitfalls with layering?
- How to design effective layering strategy?
- What are effective design patterns for layering?
- How to separate layers into areas of concerns?
- How should layers interact?
- What are the common architectural pitfalls with layering?
- How do I migrate my existing architecture to layered architecture, for example monolithic to layered architecture, from 2-tier to layered architecture or 3-tier to layered architecture?
- How should I structure my development teams i.e. group by layers or group by functional modules?
- How will my solution and project structure look like when using Layered Architecture?
- How to design effective logging and instrumentation strategy?
- What are effective logging and instrumentation patterns?
- What are the common architectural pitfalls with logging and instrumentation?
- How to design effective logging and instrumentation strategy?
- What are effective logging and instrumentation patterns?
- What are the common architectural pitfalls with logging and instrumentation?
- Which layers should implement logging?
- When do I need a custom logging mechanism?
- How to make logging configurable?
- How to secure of logged data?
- How to design effective State Management?
- What are common architectural pitfalls with state management?
- Which components should be stateful?
- Which components should be stateless?
- Where should I store state?
- What information should be cached?
- How to manage state in a web farm?
- How to protect state data?
- What are the effective strategies and patterns for structuring the applications?
- How do I divide the application into sub-system?
- How do I factor the applications into layers, components and services?
- How do I factor the application into tiers?
- What are the common architectural pitfalls with Structuring?
- What are the effective strategies and patterns for structuring the applications?
- How do I divide the application into sub-system?
- How do I factor the applications into layers, components and services?
- How do I factor the application into tiers?
- What are the common architectural pitfalls with Structuring?
- Where do you perform validation?
- What do you validate?
- How to validate business rules?
- How to protect against malicious data?
- How to handle data validation exception?
- What are the common architectural pitfalls with validations?
- How to design client side validation?
- How to determine trust boundary for validation?
- What are the different types of workflows? What are the differences between them?
- What are common workflow scenarios?
- What are effective workflow patterns?
- What are effective the tools for designing workflow?
- What is workflow modeling and how does it help in system design?
- What are the common pitfalls with using workflows?
- What is “workflow persistence”?
- How does workflow communicate with the system?
- How design for error recovery in a workflow?
- How do I manage workflow instances?
- How do I host workflows?
Task List Example
A “Task List” is simply a list of the tasks that users perform within a domain. I find it helpful to use the language “How To.” This forces people to think in terms of goals. Sometimes it’s helpful to know the goal. Sometimes it’s more helpful to know the specific tasks. When you need to up-level it, simply ask “What are you trying to accomplish?” When you need to drop down a notch, simply ask, “What are you trying to do?” You can collect tasks from users through interviews, surveys, etc. Again, I find that hands-on is one of the best ways to really build empathy for the pains and needs. The real power comes from transforming from the problem side (the pains and needs), to the solution side (the specific goal or task that would address the pain or need.)Architectural Frame Tasks List
Categories
- Authentication and Authorization
- Caching and State
- Communication
- Composition
- Concurrency and Transactions
- Configuration Management
- Coupling and Cohesion
- Data Access
- Exception Management
- Logging and Instrumentation
- User Experience
- Validation
- Workflow
- How to identify trust boundaries
- How to design single sign-on strategy
- How to design role-based authorization
- How to design resource-based authorization
- How to design claims-based authorization
- How to design a trusted sub-system
- How to flow identity across layers and tiers
- How to improve performance with caching
- How to decide what data to cache
- How to decide where to cache data
- How to cache data on client side
- How to cache user specific data
- How to manage expiration policy and scavenging mechanism
- How to protect cached data
- How to implement thread safety for the cached items using locking
- How to cache data proactively
- How to cache data reactively
- How to design caching for distributed environment
- How to design caching for web farm scenarios
- How to structure an application to optimize communication efficiency
- How to design for remote communication
- How to protect communication channels
- How to choose a communication protocol
- How to pass data across layers
- How to flow identity across layers (tiers)
- How to secure communication between the layers
- How to design for synchronous communication
- How to design for asynchronous communication
- How to design fire and forget (one way) communication
- How to design for transactions
- How to manage distributed transactions
- How to design for atomic transactions
- How to design for long running transactions
- How to design for distributed transactions
- How to choose isolation level
- How to design configuration
- How to choose for configuration store
- How to separate application data from configuration data
- How to synchronize configuration across distributed environment
- How to protective sensitive configuration information
- How to enable changing configuration information at run-time
- How to design for loose coupling across layers
- How to design for high cohesion within layers
- How to design message based interfaces
- How to design your data access layer
- How to design data abstraction
- How to pass data across layers and tiers
- How to model your data
- How to page records
- How to design for very large databases
- How to design a domain-driven architecture
- How to design a database-driven architecture
- How to manage database connections
- How to handle transactions
- How to handle concurrency
- How to design for a multi-tenant database
- How to design exception management
- How to design for unhandled exceptions
- How to design structured exception handling
- How to design for appropriate display of exception data
- How to plan and design for exception logging
- How to propagate exceptions in a distributed design
- How to design for notifications
- How to design layering
- How to choose communication options between layers
- How to design message based interfaces for remove layers
- How to separate layers into areas of concerns
- How to design logging
- How to design for instrumentation
- How to design logging for distributed environment
- How to design logging for web farm scenario
- How to configure logging
- How to secure logged data
- How to choose the appropriate state model
- How to flow call context data across application layers
- How to design State Management
- How to store state
- How to manage state in a web farm
- How to protect state data
- How to design for passing data across tiers
- How to choose patterns for structuring your application
- How to factor security requirements when structuring your application
- How to factor performance and scalability requirements when structuring your application
- How to factor the applications into layers, components and services
- How to factor the application into tiers
- How to identify trust boundaries
- How to design a centralized validation solution
- How to validate input and data against business rules
- How to validate input and data for security
- How to validate the integrity of data
- How to handle data validation exceptions
- How to design workflow
- How to choose a workflow technology
- How to choose authoring mode
Summarizing Task Lists
I’ve found it especially helpful to organize massive lists of tasks into simple two-column tables. This creates a nice view that makes it very easy to prioritize, cut, or elaborate, in a fast and simple way. You can color code your lists. You can bubble key things to the top. You can make whitespace where you need it. You can group your tasks under sub-items within a row. The choices are endless, but the two-column tables does make dealing with massive mounds of information a breeze. The way it compacts and frames information makes scanning very easy, which is important when you are trying to get the “bird’s-eye view.”| Category | Items |
| Authentication and Authorization | |
| Caching | Design
|
| Communication |
|
| Concurrency and Transactions |
|
| Configuration Management |
|
| Coupling and Cohesion |
|
| Data Access | Design
|
| Exception Management |
|
| Layering |
|
| Logging and Instrumentation |
|
| State Management |
|
| Structure |
|
| Validation |
|
| Workflow |
|
User Stories at a Glance Example
One of the most powerful techniques I use to rapidly gather user requirements is user stories. I find that capturing user stories with the language, “As a user, I need to” .. or “As a user, I want to …” really helps add context and clarity, while keeping it amazingly simple. I also find that organizing the user stories by Hot Spots helps go a long way, especially when you are dealing with a large amount of information. Below is an example where I was collecting user stories to rapidly figure out the top concerns of business leaders and Enterprise Architects when it comes to cloud computing.The beauty is that when you capture the user stories well, it is very easy to deal with both timeless stories and timely ones. In this particular example, even though it’s a few years old, you can see that the top issues that it exposes are alive and well. One additional point on this example is that I used another information pattern. I call it the “View More” pattern. I use it to bubble up the short-list and then push the rest of the list below the “View More …” heading. It’s highly effective for organizing very large information sets, especially if you alphabetize the list.
User Stories for Cloud Enterprise Strategy
Categories
- Awareness / Education
- Architecture
- Availability
- Competition
- Cost
- Governance and Regulation
- Industry
- Integration
- Operations
- People
- Performance
- Planning
- Risk
- Security
- Service Levels / Quality of Service
- Solutions
- Sourcing
- Strategy
- Support
| Category | Items |
| Awareness / Education |
View More…
|
| Architecture |
View More…
|
| Availability |
View More…
|
| Competition |
|
| Cost |
View More…
|
| Governance and Regulation |
View More…
|
| Industry |
|
| Integration |
|
| Operations |
View More…
|
| People |
View More…
|
| Performance |
View More…
|
| Planning |
View More…
|
| Risk |
View More…
|
| Security |
View More…
|
| Service Levels / Quality of Service |
View More…
|
| Solutions |
View More…
|
| Sourcing |
|
| Strategy |
|
| Support |
View More…
|
Monday, April 2, 2012
Efficient File I/O From C#
Efficient File I/O From C#:
This article describes and benchmarks different ways to do file I/O from C#. All of the code referenced in this article is available for download and is free to use.
There are many different ways to do file I/O in C#. The .NET framework provides the following classes:
WinFileIO – provides the capability to utilize the ReadFile and Writefile windows IO functions. Based upon extensive testing, these functions are the most efficient way to perform file I/O from C# and C++.
How The Test Program Works
The user interface provides the following buttons:
Each test is consistent in the way benchmarking is done. For each test function, the current time is obtained before the I/O function begins, and is then retrieved right after the test ends. Benchmarking includes the time it takes to open and close the file, except for the last set of tests which only measure the time it takes to read or write the files.
Each test consists of reading in or writing out 3 files:
File class methods:
Read(1) - reads the entire contents of the file into a character array using the single argument constructor. See TestStreamReader1.
File class methods:
This class was designed to make it easy to use the Windows ReadFile and WriteFile methods. It handles all of the unsafe operations with pointers. Calling methods do not have to be declared unsafe. Implements the IDisposable interface which means that the Dispose method should be called when the object is no longer needed. If there is a problem with any method in this class, it will throw an exception with the Windows error information. If the function returns, then this indicates success. The only exception to this is the Close method.
Constructors:
Running the read file tests:
Total time reading < 1MB with File.ReadAllLines = 00:00:00.0030002
Total time reading 10MB with File.ReadAllLines = 00:00:00.0640037
Total time reading 50MB with File.ReadAllLines = 00:00:00.3540202
Total time reading < 1MB with File.ReadAllText = 00:00:00.0040002
Total time reading 10MB with File.ReadAllText = 00:00:00.0360020
Total time reading 50MB with File.ReadAllText = 00:00:00.1630093
Total time reading < 1MB with File.ReadAllBytes = 00:00:00
Total time reading 10MB with File.ReadAllBytes = 00:00:00.0050003
Total time reading 50MB with File.ReadAllBytes = 00:00:00.0260015
Total time reading < 1MB with BinaryReader.Read = 00:00:00.0020001
Total time reading 10MB with BinaryReader.Read = 00:00:00.0270016
Total time reading 50MB with BinaryReader.Read = 00:00:00.1260072
Total time reading < 1MB with StreamReader1.Read = 00:00:00.0010001
Total time reading 10MB with StreamReader1.Read = 00:00:00.0200011
Total time reading 50MB with StreamReader1.Read = 00:00:00.0960055
Total time reading < 1MB with StreamReader2.Read(large buf) = 00:00:00.0010001
Total time reading 10MB with StreamReader2.Read(large buf) = 00:00:00.0160009
Total time reading 50MB with StreamReader2.Read(large buf) = 00:00:00.0750043
Total time reading < 1MB with StreamReader3.ReadBlock = 00:00:00.0010001
Total time reading 10MB with StreamReader3.ReadBlock = 00:00:00.0150008
Total time reading 50MB with StreamReader3.ReadBlock = 00:00:00.0750043
Total time reading < 1MB with StreamReader4.ReadToEnd = 00:00:00.0020001
Total time reading 10MB with StreamReader4.ReadToEnd = 00:00:00.0320018
Total time reading 50MB with StreamReader4.ReadToEnd = 00:00:00.1720099
Total time reading < 1MB with mult StreamReader5.Read = 00:00:00.0020001
Total time reading 10MB with mult StreamReader5.Read = 00:00:00.0430025
Total time reading 50MB with mult StreamReader5.Read = 00:00:00.0850048
Total time reading < 1MB with StreamReader6.ReadLine = 00:00:00.0020002
Total time reading 10MB with StreamReader6.ReadLine = 00:00:00.0310017
Total time reading 50MB with StreamReader6.ReadLine = 00:00:00.1510087
Total time reading < 1MB with StreamReader7.Read parsing = 00:00:00.1470084
Total time reading 10MB with StreamReader7.Read parsing = 00:00:00.1600091
Total time reading 50MB with StreamReader7.Read parsing = 00:00:00.2260129
Total time reading < 1MB with FileStream1.Read no parsing = 00:00:00.0080005
Total time reading 10MB with FileStream1.Read no parsing = 00:00:00.0040002
Total time reading 50MB with FileStream1.Read no parsing = 00:00:00.0190011
Total time reading < 1MB with FileStream2.Read parsing = 00:00:00.1220070
Total time reading 10MB with FileStream2.Read parsing = 00:00:00.1220069
Total time reading 50MB with FileStream2.Read parsing = 00:00:00.1370079
Total time reading < 1MB with multiFileStream2A.Read parsing = 00:00:00.1180067
Total time reading 10MB with multiFileStream2A.Read parsing = 00:00:00.1210070
Total time reading 50MB with multiFileStream2A.Read parsing = 00:00:00.1320075
Total time reading < 1MB with FileStream3.Read(Rand) no parsing= 00:00:00
Total time reading 10MB with FileStream3.Read(Rand) no parsing = 0:00:00.0030002
Total time reading 50MB with FileStream3.Read(Rand) no parsing = 00:00:00.0170009
Total time reading < 1MB with FileStream4.BeginRead no parsing = 0:00:00.0020001
Total time reading 10MB with FileStream4.BeginRead no parsing = 0:00:00.0040002
Total time reading 50MB with FileStream4.BeginRead no parsing = 00:00:00.0180011
Total time reading < 1MB with FileStream5.BeginRead parsing = 0:00:00.0020001
Total time reading 10MB with FileStream5.BeginRead parsing = 0:00:00.0280016
Total time reading 50MB with FileStream5.BeginRead parsing = 0:00:00.1370079
Total time reading < 1MB with FileStream6.BeginRead parsing = 00:00:00.0030002
Total time reading 10MB with FileStream6.BeginRead parsing = 00:00:00.0280016
Total time reading 50MB with FileStream6.BeginRead parsing = 00:00:00.1360077
Total time reading < 1MB with FileStream7.BeginRead = 00:00:00
Total time reading 10MB with FileStream7.BeginRead = 00:00:00.0050003
Total time reading 50MB with FileStream7.BeginRead = 00:00:00.0240014
Total time reading < 1MB with FileStream8.BeginRead parsing = 00:00:00.0020001
Total time reading 10MB with FileStream8.BeginRead parsing = 00:00:00.0310018
Total time reading 50MB with FileStream8.BeginRead parsing = 00:00:00.1480085
Total time reading < 1MB with WFIO1.Read No Parsing = 00:00:00.0020001
Total time reading 10MB with WFIO1.Read No Parsing = 00:00:00.0020001
Total time reading 50MB with WFIO1.Read No Parsing = 00:00:00.0120007
Total time reading < 1MB with WFIO2.ReadUntilEOF No Parsing = 00:00:00.0010001
Total time reading 10MB with WFIO2.ReadUntilEOF No Parsing = 00:00:00.0030001
Total time reading 50MB with WFIO2.ReadUntilEOF No Parsing = 00:00:00.0140008
Total time reading < 1MB with WFIO3.ReadBlocks API No Parsing= 00:00:00.0010001
Total time reading 10MB with WFIO3.ReadBlocks API No Parsing = 00:00:00.0030002
Total time reading 50MB with WFIO3.ReadBlocks API No Parsing = 00:00:00.0130008
Total time reading < 1MB with BinaryReader.Read = 00:00:00.0010001
Total time reading 10MB with BinaryReader.Read = 00:00:00.0220012
Total time reading 50MB with BinaryReader.Read = 00:00:00.1080062
Total time reading < 1MB with StreamReader2.Read(large buf) = 00:00:00.0010001
Total time reading 10MB with StreamReader2.Read(large buf) = 00:00:00.0150008
Total time reading 50MB with StreamReader2.Read(large buf) = 00:00:00.0690040
Total time reading < 1MB with FileStream1.Read no parsing = 00:00:00.0010000
Total time reading 10MB with FileStream1.Read no parsing = 00:00:00.0030002
Total time reading 50MB with FileStream1.Read no parsing = 00:00:00.0130008
Total time reading < 1MB with WFIO.Read No Open/Close = 00:00:00.0010001
Total time reading 10MB with WFIO.Read No Open/Close = 00:00:00.0030001
Total time reading 50MB with WFIO.Read No Open/Close = 00:00:00.0130008
Read file tests have completed.
Analysis Of Read Results:
The File class provides the simplest way to read in a class. The ReadAllBytes method of this class provides a fairly efficient way to read in a file and is only bested by the read methods in the FileStream and WinFileIO classes. From the results, it appears that the best StreamReader and BinaryReader read methods are roughly 3 to 5 times slower than the ReadAllBytes method.
The FileStream read methods were shown to be the fastest way to read a file into memory using a method from the .NET Framework. The synchronous method of reading the entire file into memory in TestFileStreamRead1 and TestFileStreamRead3 proved to be the best of this set of tests with TestFileStreamRead3 taking top honors by a hair. The only difference between these two tests is that the file is opened with the SequentialScan option in TestFileStreamRead1 .vs. opening the file with RandomAccess in TestFileStreamRead3. Since there are always other OS activities going on while running a benchmark, it is hard to know if one method is superior to another when it is this close. However, these tests have been tested on other systems multiple times with different Windows OSs with the same results, so in this case it appears that the TestFileStreamRead3 method is marginally superior.
The biggest disappointment came with the 5 FileStream asynchronous tests given in TestFileStreamRead4 – TestFileStreamRead8. These tests all show that reading in a file asynchronously is inferior to reading it in synchronously. This is even true if other activities like parsing a file is done in between reads. For example, compare the results of TestFileStreamRead2A which reads in a file synchronously and parses the data against the results of TestFileStreamRead5 which reads in a file asynchronously and parses the data while the next block is read in asynchronously. Even when the locks have been removed (see TestFileStreamRead8), it is still at least 10% slower than reading the file in synchronously and then parsing the file afterwards (see TestFileStreamRead2A).
The WinFileIO class proved to be the fastest way to read a file in. It is between 33% to 50% faster than the fastest FileStream read method based upon the measured times above. However, the last set of tests – TestBinaryReader1NoOpenClose through TestReadFileWinAPINoOpenClose measure how quickly the files are read in after the file is opened. According to the results, the FileStream read method is just as fast as any of the WinFileIO read methods. So, it looks like the .NET framework takes longer to open a file than the windows CreateFile function.
Benchmark Write Results:
Running the write file Tests:
Total time writing < 1MB with File.WriteAllLines = 00:00:00.0050003
Total time writing 10MB with File.WriteAllLines = 00:00:00.0350020
Total time writing 50MB with File.WriteAllLines = 00:00:00.1620093
Total time writing < 1MB with File.TestWriteAllText = 00:00:00.0040002
Total time writing 10MB with File.TestWriteAllText = 00:00:00.0270016
Total time writing 50MB with File.TestWriteAllText = 00:00:00.1440082
Total time writing < 1MB with File.WriteAllBytes = 00:00:00.3560204
Total time writing 10MB with File.WriteAllBytes = 00:00:00.3390194
Total time writing 50MB with File.WriteAllBytes = 00:00:00.3530202
Total time writing < 1MB with BinaryWriter.Write = 00:00:00.0010001
Total time writing 10MB with BinaryWriter.Write = 00:00:00.0050003
Total time writing 50MB with BinaryWriter.Write = 00:00:00.3040174
Total time writing < 1MB with StreamWriter1.Write = 00:00:00.0030002
Total time writing 10MB with StreamWriter1.Write = 00:00:00.0230013
Total time writing 50MB with StreamWriter1.Write = 00:00:00.1140065
Total time writing < 1MB with FileStream1.Write no parsing = 00:00:00.0010001
Total time writing 10MB with FileStream1.Write no parsing = 00:00:00.0050003
Total time writing 50MB with FileStream1.Write no parsing = 00:00:00.3670210
Total time writing < 1MB with FileStream2.Write no parsing = 00:00:00.0070004
Total time writing 10MB with FileStream2.Write no parsing = 00:00:00.1060061
Total time writing 50MB with FileStream2.Write no parsing = 00:00:00.5000286
Total time writing < 1MB with FileStream3.Write no parsing = 00:00:00.0100006
Total time writing 10MB with FileStream3.Write no parsing = 00:00:00.1150066
Total time writing 50MB with FileStream3.Write no parsing = 00:00:00.5840334
Total time writing < 1MB with WFIO1.Write No Parsing = 00:00:00.0020001
Total time writing 10MB with WFIO1.Write No Parsing = 00:00:00.0050003
Total time writing 50MB with WFIO1.Write No Parsing = 00:00:00.3530202
Total time writing < 1MB with WFIO2.WriteBlocks No Parsing = 00:00:00.0010001
Total time writing 10MB with WFIO2.WriteBlocks No Parsing = 00:00:00.0060003
Total time writing 50MB with WFIO2.WriteBlocks No Parsing = 00:00:00.0260015
Write file tests have completed.
ANALYSIS OF WRITE RESULTS:
The File class provides the simplest way to write a file out. Unlike the ReadAllBytes method for reading files, WriteAllBytes is less efficient than WriteAllText according to the results above.
One interesting result is that the times to write out the < 1MB file and 10 MB file for the BinaryWriter, FileStream, and WinFileIO classes are quite similar and fast. However, the time to write out the 50 MB file takes around 60 times longer than the 10 MB file. This does not apply to the WinFileIO.WriteBlocks method which proved to be the fastest way to write a file out. The most likely reason for this is that WriteBlocks writes the file out in 65,536 byte chunks. However, the TestFileStreamWrite3 test also writes out the file in 65,536 byte chunks and proved to be the slowest method. I can’t think of a good explanation for this other than perhaps the FileStream.Write method has some issues.
Conclusion:
Any time a benchmark is done trying to test out file I/O methods, it is very difficult to completely trust the results due to the operating system caching files and other OS activities. If different files are used, then they can be placed on areas of the drive that will yield better performance simply because the drive can access them faster and can impact the results. So, take a little grain of salt with these benchmark results. To achieve the best performance for your environment, I would recommend trying out different classes in your production environment to see which yields the best performance.
Having said that and after testing on 3 different machines with similar results, I believe that the best performing file I/O can be obtained from the WinFileIO read methods for reading a file and the WinFileIO.WriteBlocks method for writing files.
I have done similar tests with C++ which are not shown here and believe that the Windows ReadFile and WriteFile methods are the most efficient way to do file I/O from that language as well.
Download and installation:
Click this link or the download button near the top right of this post to download the file. This file is a zipped file containing 2 visual studio projects. Extract it into the folder of your choice and leave the folder hierarchy intact. The code was built and tested with Visual Studio 2008, but it should work with Visual Studio 2010 with little if any modification. To make it work with previous versions of Visual Studio, you may have to open a new project and add the individual files to each project.
The following code files are contained in the in the FileTestsForEfficiency folder:
This article describes and benchmarks different ways to do file I/O from C#. All of the code referenced in this article is available for download and is free to use.
There are many different ways to do file I/O in C#. The .NET framework provides the following classes:
- File – Provides static methods for the creation, opening of files, copying, deleting, moving, reading, and writing of files.
- BinaryReader – Reads primitive data types as binary values using a specific encoding.
- BinaryWriter – Writes primitive types in binary to a stream and supports writing strings using a specific encoding.
- StreamReader – Implements a TextReader that reads characters from a byte stream using a particular encoding.
- StreamWriter – Implements a TextWriter for writing characters to a stream using a particular encoding.
- FileStream – Exposes a Stream around a file, supporting both synchronous and asynchronous read and write operations.
- ReadFile – Reads data from a file. This function supports both synchronous and asynchronous operations.
- WriteFile – Writes data to a file. This function supports both synchronous and asynchronous operation.
WinFileIO – provides the capability to utilize the ReadFile and Writefile windows IO functions. Based upon extensive testing, these functions are the most efficient way to perform file I/O from C# and C++.
How The Test Program Works
The user interface provides the following buttons:
- Run All - tests the Read File and Write File functions. Similar to pressing the Read File and Write File buttons.
- Read File - tests the read file methods for each class listed above. Each test consists of reading in 3 text files with sizes of roughly < 1mb, 10mb, and 50mb.
- Write File - tests the write file methods for each class listed above. Each test consists of reading in 3 text files with sizes of roughly < 1mb, 10mb, and 50mb.
- WinFileIO Unit Tests - tests each public interface I/O method in the class to show that it works correctly.
Each test is consistent in the way benchmarking is done. For each test function, the current time is obtained before the I/O function begins, and is then retrieved right after the test ends. Benchmarking includes the time it takes to open and close the file, except for the last set of tests which only measure the time it takes to read or write the files.
Each test consists of reading in or writing out 3 files:
- < 1 MB - approximately .66 megabytes.
- 10 MB - approximately 10 megabytes.
- 50 MB - approximately 50 megabytes.
- CPU – INTEL i7-950.
- Memory – 6 GB
- OS – Windows 7 Pro which is contained on a solid state drive.
- Hard drive - Western Digital 1TB SATA III 6GB/S 7200RPM 64MB CACHE, which is where the data files reside.
- IDE – Visual Studio 2008 standard edition with .NET framework version 3.5 SP1.
File class methods:
- ReadAllLines – reads all lines of the file into a string array. See TestReadAllLines.
- ReadAllText - reads the entire contents of the file into a string. See TestReadAllText.
- ReadAllBytes - reads the entire contents of the file into a byte array. See TestReadAllBytes.
- Read - reads the entire contents of the file into a character array. See TestBinaryReader1.
Read(1) - reads the entire contents of the file into a character array using the single argument constructor. See TestStreamReader1.
- Read(2) – reads the entire contents of the file into a character array. Uses a constructor to specify a sector aligned buffer size. See TestStreamReader2.
- Read(3) – a loop is used to read in the entire contents of the file into a character array. Uses a constructor to specify a sector aligned buffer size. The loop is terminated when the Peek function indicates there is no more data.
- ReadBlock - reads the entire contents of the file into a character array. Uses a constructor to specify a sector aligned buffer size. See TestStreamReader5.
- ReadToEnd - a loop is used to read in the entire contents of the file into a character array. Uses a constructor to specify a sector aligned buffer size. The loop is terminated when the Peek function indicates there is no more data. See TestStreamReader4.
- ReadBlock - reads the entire contents of the file into a character array. Uses a constructor to specify a sector aligned buffer See TestStreamReader3.
- ReadLine - reads the entire contents of the file line by line into a string. Uses a constructor to specify a sector aligned buffer size. See TestStreamReader3.
- Read(1) – reads the entire contents of the file into a byte array. See TestFileStreamRead1.
- Read(2) - reads the entire contents of the file into a byte array and parse into lines. See TestFileStreamRead2.
- Read(3) – reads the entire contents of the file into a byte array and parse into lines. See TestFileStreamRead2A.
- Read(4) - reads the entire contents of the file into a byte array. Uses the RandomAccess option in the constructor to determine if there is any impact on performance. See TestFileStreamRead3.
- BeginRead(1) – reads the entire contents of the file into a byte array asynchronously. See TestFileStreamRead4.
- BeginRead(2) – reads the entire contents of the file into a byte array asynchronously and parses into lines. See TestFileStreamRead5.
- BeginRead(3) – reads the entire contents of the file into a byte array asynchronously and parses into lines. Identical to TestFileStreamRead5 except for using a different threading locking mechanism. See TestFileStreamRead6.
- BeginRead(4) - reads the entire contents of the file into a byte array asynchronously. Uses no locking mechanism. See TestFileStreamRead7.
- BeginRead(5) - reads the entire contents of the file into a byte array asynchronously and parses into lines. See TestFileStreamRead8.
- Read - reads the specified number of bytes into an array.
- ReadUntilEOF - reads the entire contents of a file into an array.
- ReadBlocks - reads the specified number of bytes into an array.
File class methods:
- WriteAllLines - writes all lines in a string array to a file. See TestWriteAllLines.
- WriteAllText - writes the entire contents in a string to a file. See TestWriteAllText.
- WriteAllBytes - writes the entire contents of a byte buffer to a file. See TestWriteAllBytes.
- Write - writes the entire contents of a character array to a file. See TestBinaryWriter1.
- Write - writes the entire contents a character array to a file. See TestStreamWriter1.
- Write(1) - writes the entire contents of a byte array to a file. See TestFileStreamWrite1.
- Write(2) - writes the entire contents of a byte array to a file. See TestFileStreamWrite2.
- Write(3) - writes the entire contents of a byte array to a file. See TestFileStreamWrite3.
- Write - writes the entire contents of an array to a file. See TestWriteFileWinAPI1.
- WriteBlocks - writes the entire contents of an array to a file. See TestWriteFileWinAPI2.
This class was designed to make it easy to use the Windows ReadFile and WriteFile methods. It handles all of the unsafe operations with pointers. Calling methods do not have to be declared unsafe. Implements the IDisposable interface which means that the Dispose method should be called when the object is no longer needed. If there is a problem with any method in this class, it will throw an exception with the Windows error information. If the function returns, then this indicates success. The only exception to this is the Close method.
Constructors:
- WinFileIO() - default. If this constructor is used, then the PinBuffer function must be called.
- WinFileIO(Array Buffer) – this constructor should be used most of the time. The Buffer is used to read in or write out the data. The array passed in can be of any type provided it does not contain any references or pointers. So, byte, char, int, long, and double arrays should all work. But string arrays will not since strings use pointers. The code has only been tested with byte arrays.
- void PinBuffer(Array Buffer) – this method pins the buffer in memory and retrieves a pointer to it which is used for all I/O operations. UnpinBuffer is called by this function so it need not be called by the user. This function only needs to be called if the default constructor is used or a different buffer needs to be used for reading or writing.
- void OpenForReading(string FileName) – opens a file for reading. The argument FileName must contain the path and filename of the file to be read.
- void OpenForWriting(string FileName) – opens a file for writing. If the file exists, it will be overwritten.
- int Read(int BytesToRead) – reads in a file up to BytesToRead The return value is the number of bytes read. BytesToRead must not be larger than the size of the buffer specified in the constructor or PinBuffer.
- int ReadUntilEOF() - reads in the entire contents of the file. The file must be <= 2GB. If the buffer is not large enough to read the file, then an ApplicationException will be thrown. No check is made to see if the buffer is large enough to hold the file. If this is needed, then use the ReadBlocks method.
- int ReadBlocks(int BytesToRead) – reads a total of BytesToRead at a time. There is a limit of 2gb per call. BytesToRead should not be larger than the size of the buffer specified in the constructor or PinBuffer.
- int Write(int BytesToWrite) – writes a buffer out to a file. The return value is the number of bytes written to the file.
- int WriteBlocks(int NumBytesToWrite) - writes a buffer out to a file. The return value is the number of bytes written to the file.
- bool Close() - closes the file. If this method succeeds, then true is returned. Otherwise, false is returned.
Running the read file tests:
Total time reading < 1MB with File.ReadAllLines = 00:00:00.0030002
Total time reading 10MB with File.ReadAllLines = 00:00:00.0640037
Total time reading 50MB with File.ReadAllLines = 00:00:00.3540202
Total time reading < 1MB with File.ReadAllText = 00:00:00.0040002
Total time reading 10MB with File.ReadAllText = 00:00:00.0360020
Total time reading 50MB with File.ReadAllText = 00:00:00.1630093
Total time reading < 1MB with File.ReadAllBytes = 00:00:00
Total time reading 10MB with File.ReadAllBytes = 00:00:00.0050003
Total time reading 50MB with File.ReadAllBytes = 00:00:00.0260015
Total time reading < 1MB with BinaryReader.Read = 00:00:00.0020001
Total time reading 10MB with BinaryReader.Read = 00:00:00.0270016
Total time reading 50MB with BinaryReader.Read = 00:00:00.1260072
Total time reading < 1MB with StreamReader1.Read = 00:00:00.0010001
Total time reading 10MB with StreamReader1.Read = 00:00:00.0200011
Total time reading 50MB with StreamReader1.Read = 00:00:00.0960055
Total time reading < 1MB with StreamReader2.Read(large buf) = 00:00:00.0010001
Total time reading 10MB with StreamReader2.Read(large buf) = 00:00:00.0160009
Total time reading 50MB with StreamReader2.Read(large buf) = 00:00:00.0750043
Total time reading < 1MB with StreamReader3.ReadBlock = 00:00:00.0010001
Total time reading 10MB with StreamReader3.ReadBlock = 00:00:00.0150008
Total time reading 50MB with StreamReader3.ReadBlock = 00:00:00.0750043
Total time reading < 1MB with StreamReader4.ReadToEnd = 00:00:00.0020001
Total time reading 10MB with StreamReader4.ReadToEnd = 00:00:00.0320018
Total time reading 50MB with StreamReader4.ReadToEnd = 00:00:00.1720099
Total time reading < 1MB with mult StreamReader5.Read = 00:00:00.0020001
Total time reading 10MB with mult StreamReader5.Read = 00:00:00.0430025
Total time reading 50MB with mult StreamReader5.Read = 00:00:00.0850048
Total time reading < 1MB with StreamReader6.ReadLine = 00:00:00.0020002
Total time reading 10MB with StreamReader6.ReadLine = 00:00:00.0310017
Total time reading 50MB with StreamReader6.ReadLine = 00:00:00.1510087
Total time reading < 1MB with StreamReader7.Read parsing = 00:00:00.1470084
Total time reading 10MB with StreamReader7.Read parsing = 00:00:00.1600091
Total time reading 50MB with StreamReader7.Read parsing = 00:00:00.2260129
Total time reading < 1MB with FileStream1.Read no parsing = 00:00:00.0080005
Total time reading 10MB with FileStream1.Read no parsing = 00:00:00.0040002
Total time reading 50MB with FileStream1.Read no parsing = 00:00:00.0190011
Total time reading < 1MB with FileStream2.Read parsing = 00:00:00.1220070
Total time reading 10MB with FileStream2.Read parsing = 00:00:00.1220069
Total time reading 50MB with FileStream2.Read parsing = 00:00:00.1370079
Total time reading < 1MB with multiFileStream2A.Read parsing = 00:00:00.1180067
Total time reading 10MB with multiFileStream2A.Read parsing = 00:00:00.1210070
Total time reading 50MB with multiFileStream2A.Read parsing = 00:00:00.1320075
Total time reading < 1MB with FileStream3.Read(Rand) no parsing= 00:00:00
Total time reading 10MB with FileStream3.Read(Rand) no parsing = 0:00:00.0030002
Total time reading 50MB with FileStream3.Read(Rand) no parsing = 00:00:00.0170009
Total time reading < 1MB with FileStream4.BeginRead no parsing = 0:00:00.0020001
Total time reading 10MB with FileStream4.BeginRead no parsing = 0:00:00.0040002
Total time reading 50MB with FileStream4.BeginRead no parsing = 00:00:00.0180011
Total time reading < 1MB with FileStream5.BeginRead parsing = 0:00:00.0020001
Total time reading 10MB with FileStream5.BeginRead parsing = 0:00:00.0280016
Total time reading 50MB with FileStream5.BeginRead parsing = 0:00:00.1370079
Total time reading < 1MB with FileStream6.BeginRead parsing = 00:00:00.0030002
Total time reading 10MB with FileStream6.BeginRead parsing = 00:00:00.0280016
Total time reading 50MB with FileStream6.BeginRead parsing = 00:00:00.1360077
Total time reading < 1MB with FileStream7.BeginRead = 00:00:00
Total time reading 10MB with FileStream7.BeginRead = 00:00:00.0050003
Total time reading 50MB with FileStream7.BeginRead = 00:00:00.0240014
Total time reading < 1MB with FileStream8.BeginRead parsing = 00:00:00.0020001
Total time reading 10MB with FileStream8.BeginRead parsing = 00:00:00.0310018
Total time reading 50MB with FileStream8.BeginRead parsing = 00:00:00.1480085
Total time reading < 1MB with WFIO1.Read No Parsing = 00:00:00.0020001
Total time reading 10MB with WFIO1.Read No Parsing = 00:00:00.0020001
Total time reading 50MB with WFIO1.Read No Parsing = 00:00:00.0120007
Total time reading < 1MB with WFIO2.ReadUntilEOF No Parsing = 00:00:00.0010001
Total time reading 10MB with WFIO2.ReadUntilEOF No Parsing = 00:00:00.0030001
Total time reading 50MB with WFIO2.ReadUntilEOF No Parsing = 00:00:00.0140008
Total time reading < 1MB with WFIO3.ReadBlocks API No Parsing= 00:00:00.0010001
Total time reading 10MB with WFIO3.ReadBlocks API No Parsing = 00:00:00.0030002
Total time reading 50MB with WFIO3.ReadBlocks API No Parsing = 00:00:00.0130008
Total time reading < 1MB with BinaryReader.Read = 00:00:00.0010001
Total time reading 10MB with BinaryReader.Read = 00:00:00.0220012
Total time reading 50MB with BinaryReader.Read = 00:00:00.1080062
Total time reading < 1MB with StreamReader2.Read(large buf) = 00:00:00.0010001
Total time reading 10MB with StreamReader2.Read(large buf) = 00:00:00.0150008
Total time reading 50MB with StreamReader2.Read(large buf) = 00:00:00.0690040
Total time reading < 1MB with FileStream1.Read no parsing = 00:00:00.0010000
Total time reading 10MB with FileStream1.Read no parsing = 00:00:00.0030002
Total time reading 50MB with FileStream1.Read no parsing = 00:00:00.0130008
Total time reading < 1MB with WFIO.Read No Open/Close = 00:00:00.0010001
Total time reading 10MB with WFIO.Read No Open/Close = 00:00:00.0030001
Total time reading 50MB with WFIO.Read No Open/Close = 00:00:00.0130008
Read file tests have completed.
Analysis Of Read Results:
The File class provides the simplest way to read in a class. The ReadAllBytes method of this class provides a fairly efficient way to read in a file and is only bested by the read methods in the FileStream and WinFileIO classes. From the results, it appears that the best StreamReader and BinaryReader read methods are roughly 3 to 5 times slower than the ReadAllBytes method.
The FileStream read methods were shown to be the fastest way to read a file into memory using a method from the .NET Framework. The synchronous method of reading the entire file into memory in TestFileStreamRead1 and TestFileStreamRead3 proved to be the best of this set of tests with TestFileStreamRead3 taking top honors by a hair. The only difference between these two tests is that the file is opened with the SequentialScan option in TestFileStreamRead1 .vs. opening the file with RandomAccess in TestFileStreamRead3. Since there are always other OS activities going on while running a benchmark, it is hard to know if one method is superior to another when it is this close. However, these tests have been tested on other systems multiple times with different Windows OSs with the same results, so in this case it appears that the TestFileStreamRead3 method is marginally superior.
The biggest disappointment came with the 5 FileStream asynchronous tests given in TestFileStreamRead4 – TestFileStreamRead8. These tests all show that reading in a file asynchronously is inferior to reading it in synchronously. This is even true if other activities like parsing a file is done in between reads. For example, compare the results of TestFileStreamRead2A which reads in a file synchronously and parses the data against the results of TestFileStreamRead5 which reads in a file asynchronously and parses the data while the next block is read in asynchronously. Even when the locks have been removed (see TestFileStreamRead8), it is still at least 10% slower than reading the file in synchronously and then parsing the file afterwards (see TestFileStreamRead2A).
The WinFileIO class proved to be the fastest way to read a file in. It is between 33% to 50% faster than the fastest FileStream read method based upon the measured times above. However, the last set of tests – TestBinaryReader1NoOpenClose through TestReadFileWinAPINoOpenClose measure how quickly the files are read in after the file is opened. According to the results, the FileStream read method is just as fast as any of the WinFileIO read methods. So, it looks like the .NET framework takes longer to open a file than the windows CreateFile function.
Benchmark Write Results:
Running the write file Tests:
Total time writing < 1MB with File.WriteAllLines = 00:00:00.0050003
Total time writing 10MB with File.WriteAllLines = 00:00:00.0350020
Total time writing 50MB with File.WriteAllLines = 00:00:00.1620093
Total time writing < 1MB with File.TestWriteAllText = 00:00:00.0040002
Total time writing 10MB with File.TestWriteAllText = 00:00:00.0270016
Total time writing 50MB with File.TestWriteAllText = 00:00:00.1440082
Total time writing < 1MB with File.WriteAllBytes = 00:00:00.3560204
Total time writing 10MB with File.WriteAllBytes = 00:00:00.3390194
Total time writing 50MB with File.WriteAllBytes = 00:00:00.3530202
Total time writing < 1MB with BinaryWriter.Write = 00:00:00.0010001
Total time writing 10MB with BinaryWriter.Write = 00:00:00.0050003
Total time writing 50MB with BinaryWriter.Write = 00:00:00.3040174
Total time writing < 1MB with StreamWriter1.Write = 00:00:00.0030002
Total time writing 10MB with StreamWriter1.Write = 00:00:00.0230013
Total time writing 50MB with StreamWriter1.Write = 00:00:00.1140065
Total time writing < 1MB with FileStream1.Write no parsing = 00:00:00.0010001
Total time writing 10MB with FileStream1.Write no parsing = 00:00:00.0050003
Total time writing 50MB with FileStream1.Write no parsing = 00:00:00.3670210
Total time writing < 1MB with FileStream2.Write no parsing = 00:00:00.0070004
Total time writing 10MB with FileStream2.Write no parsing = 00:00:00.1060061
Total time writing 50MB with FileStream2.Write no parsing = 00:00:00.5000286
Total time writing < 1MB with FileStream3.Write no parsing = 00:00:00.0100006
Total time writing 10MB with FileStream3.Write no parsing = 00:00:00.1150066
Total time writing 50MB with FileStream3.Write no parsing = 00:00:00.5840334
Total time writing < 1MB with WFIO1.Write No Parsing = 00:00:00.0020001
Total time writing 10MB with WFIO1.Write No Parsing = 00:00:00.0050003
Total time writing 50MB with WFIO1.Write No Parsing = 00:00:00.3530202
Total time writing < 1MB with WFIO2.WriteBlocks No Parsing = 00:00:00.0010001
Total time writing 10MB with WFIO2.WriteBlocks No Parsing = 00:00:00.0060003
Total time writing 50MB with WFIO2.WriteBlocks No Parsing = 00:00:00.0260015
Write file tests have completed.
ANALYSIS OF WRITE RESULTS:
The File class provides the simplest way to write a file out. Unlike the ReadAllBytes method for reading files, WriteAllBytes is less efficient than WriteAllText according to the results above.
One interesting result is that the times to write out the < 1MB file and 10 MB file for the BinaryWriter, FileStream, and WinFileIO classes are quite similar and fast. However, the time to write out the 50 MB file takes around 60 times longer than the 10 MB file. This does not apply to the WinFileIO.WriteBlocks method which proved to be the fastest way to write a file out. The most likely reason for this is that WriteBlocks writes the file out in 65,536 byte chunks. However, the TestFileStreamWrite3 test also writes out the file in 65,536 byte chunks and proved to be the slowest method. I can’t think of a good explanation for this other than perhaps the FileStream.Write method has some issues.
Conclusion:
Any time a benchmark is done trying to test out file I/O methods, it is very difficult to completely trust the results due to the operating system caching files and other OS activities. If different files are used, then they can be placed on areas of the drive that will yield better performance simply because the drive can access them faster and can impact the results. So, take a little grain of salt with these benchmark results. To achieve the best performance for your environment, I would recommend trying out different classes in your production environment to see which yields the best performance.
Having said that and after testing on 3 different machines with similar results, I believe that the best performing file I/O can be obtained from the WinFileIO read methods for reading a file and the WinFileIO.WriteBlocks method for writing files.
I have done similar tests with C++ which are not shown here and believe that the Windows ReadFile and WriteFile methods are the most efficient way to do file I/O from that language as well.
Download and installation:
Click this link or the download button near the top right of this post to download the file. This file is a zipped file containing 2 visual studio projects. Extract it into the folder of your choice and leave the folder hierarchy intact. The code was built and tested with Visual Studio 2008, but it should work with Visual Studio 2010 with little if any modification. To make it work with previous versions of Visual Studio, you may have to open a new project and add the individual files to each project.
The following code files are contained in the in the FileTestsForEfficiency folder:
- TestsForEfficiency.cs - entry point to the application.
- MainForm.cs - holds the UI designer and button events.
- FileEffTests.cs - holds the file I/O benchmark tests, which is contained in the FileEfficientTests class.
- Win32FileIO.cs - holds the class used to implement the Windows ReadFile and WriteFile functionality.
- WinFileIOUnitTests.cs - holds the unit tests that test out the I/O methods of the WinFileIO class.
- Win32FileIO.cs - holds the class used to implement file I/O using the Windows ReadFile and Writefile methods.
Subscribe to:
Posts (Atom)
